Skip to content

Commit

Permalink
Merge pull request #8 from raydouglass/small-fixes
Browse files Browse the repository at this point in the history
A few smaller fixes
  • Loading branch information
raydouglass authored Oct 22, 2023
2 parents 3a6c3df + 9c12b60 commit 43c8afd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,4 @@ dmypy.json
cython_debug/

.vscode/
.DS_Store
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ It also simplifies some common tasks such as converting files by wrapping comple

For example, to convert a file to H.265/HEVC with GPU acceleration and AAC audio, plus scale it down to 480p, you would normally have to run a command like:
```sh
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i test.mp4 -vf scale=-1:480 -c:v h264_nvenc -preset fast -c:a aac -c:s copy -map 0 out.mp4
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i test.mp4 -vf scale=-1:480 -c:v hevc_nvenc -preset fast -c:a aac -c:s copy -map 0 out.mp4
```
Using this toolkit, it would be this instead:
```sh
manage-media convert --hw-nv --scale 480 --vc h264 test.mp4 out.mp4
manage-media convert --hw-nv --scale 480 --vc h265 test.mp4 out.mp4
```

## Installation
Expand Down
18 changes: 13 additions & 5 deletions media_management_scripts/commands/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from media_management_scripts.convert import convert_with_config


def _bulk_convert(i, o, config, overwrite):
def _bulk_convert(i, o, config, overwrite, dry_run):
print("Starting {}".format(i))
os.makedirs(os.path.dirname(o), exist_ok=True)
convert_with_config(i, o, config, print_output=True, overwrite=overwrite)
convert_with_config(
i, o, config, print_output=True, overwrite=overwrite, dry_run=dry_run
)


class ConvertCommand(SubCommand):
Expand Down Expand Up @@ -63,6 +65,7 @@ def subexecute(self, ns):
bulk = ns["bulk"]
bulk_ext = ns["bulk_ext"]
config = convert_config_from_ns(ns)
dry_run = ns["dry_run"]

if os.path.isdir(input_to_cmd):
if bulk:
Expand All @@ -82,16 +85,21 @@ def subexecute(self, ns):
)
self._bulk(
files,
lambda i, o: _bulk_convert(i, o, config, overwrite),
lambda i, o: _bulk_convert(i, o, config, overwrite, dry_run),
["Input", "Output"],
)
else:
print("Cowardly refusing to convert a direction without --bulk flag")
print("Cowardly refusing to convert a directory without --bulk flag")
elif not overwrite and os.path.exists(output):
print("Cowardly refusing to overwrite existing file: {}".format(output))
else:
convert_with_config(
input_to_cmd, output, config, print_output=True, overwrite=overwrite
input_to_cmd,
output,
config,
print_output=True,
overwrite=overwrite,
dry_run=dry_run,
)


Expand Down
7 changes: 6 additions & 1 deletion media_management_scripts/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
execute_with_output,
ffmpeg,
nice_exe,
log_command,
)
from media_management_scripts.support.files import (
check_exists,
Expand Down Expand Up @@ -70,6 +71,7 @@ def convert_with_config(
metadata=None,
mappings=None,
use_nice=True,
dry_run=False,
):
"""
Expand Down Expand Up @@ -218,7 +220,10 @@ def convert_with_config(
args.extend(["-metadata:s:v:0", "ripped=true"])
args.append(output)

return execute(args, print_output)
if dry_run:
log_command(args, True)
else:
return execute(args, print_output)


def create_remux_args(
Expand Down
41 changes: 24 additions & 17 deletions media_management_scripts/support/executables.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ def remaining_time(self, duration: float):
return None


def log_command(args, print_output=False):
import shlex

logger.debug(f"Executing: {shlex.join(args)}")
if print_output:
print(f"Executing: {shlex.join(args)}")


def maybe_add_nice(args, use_nice=False):
if args and use_nice and nice_exe and args[0] != nice_exe:
a = [nice_exe]
a.extend(args)
return a
return args


def create_ffmpeg_callback(
cb: Callable[[FFMpegProgress], None]
) -> Callable[[str], None]:
Expand Down Expand Up @@ -136,11 +152,8 @@ def wrapper(line):
def execute_with_timeout(
args, timeout: int, use_nice=True, log_output=False
) -> Tuple[int, str]:
if use_nice:
a = [nice_exe]
a.extend(args)
args = a
logger.debug("Executing: {}".format(args))
args = maybe_add_nice(args, use_nice)
log_command(args, False)
with subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
stdout, stderr = p.communicate()
if stdout and log_output:
Expand All @@ -156,13 +169,10 @@ def execute_with_timeout(


def execute_with_output(args, print_output=False, use_nice=True) -> Tuple[int, str]:
if use_nice and nice_exe:
a = [nice_exe]
a.extend(args)
args = a
logger.debug("Executing: {}".format(args))
if print_output:
print("Executing: {}".format(" ".join([str(a) for a in args])))
if not args:
raise ValueError("No args provided")
args = maybe_add_nice(args, use_nice)
log_command(args, print_output)
if DEBUG_MODE:
logger.debug("Debug mod enabled, skipping actual execution")
return 0
Expand Down Expand Up @@ -201,11 +211,8 @@ def execute_with_output(args, print_output=False, use_nice=True) -> Tuple[int, s
def execute_with_callback(
args: List[str], callback: Callable[[str], None], use_nice: bool = True
) -> int:
if use_nice and nice_exe:
a = [nice_exe]
a.extend(args)
args = a
logger.debug("Executing: {}".format(args))
args = maybe_add_nice(args, use_nice)
log_command(args, False)
with subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as p:
output = StringIO()

Expand Down

0 comments on commit 43c8afd

Please sign in to comment.