|
19 | 19 |
|
20 | 20 | """ |
21 | 21 |
|
| 22 | +from os import path |
22 | 23 | from threading import Thread as _Thread |
23 | 24 | import subprocess as _sp |
| 25 | +from copy import deepcopy |
24 | 26 | from subprocess import PIPE, DEVNULL |
| 27 | +from tempfile import TemporaryDirectory |
25 | 28 | from .ffmpeg import exec, parse |
26 | 29 | from .threading import ProgressMonitorThread |
27 | 30 | from .configure import move_global_options |
@@ -123,6 +126,7 @@ class instance. If output is piped, :code:`stdout` is default to :code:`ffmpegi |
123 | 126 | def __init__( |
124 | 127 | self, |
125 | 128 | ffmpeg_args, |
| 129 | + *, |
126 | 130 | hide_banner=True, |
127 | 131 | progress=None, |
128 | 132 | overwrite=None, |
@@ -251,6 +255,7 @@ def send_signal(self, sig: int): |
251 | 255 |
|
252 | 256 | def run( |
253 | 257 | ffmpeg_args, |
| 258 | + *, |
254 | 259 | hide_banner=True, |
255 | 260 | progress=None, |
256 | 261 | overwrite=None, |
@@ -331,3 +336,140 @@ def run( |
331 | 336 | ret.stderr = ret.stderr.decode("utf-8") |
332 | 337 |
|
333 | 338 | return ret |
| 339 | + |
| 340 | + |
| 341 | +def run_two_pass( |
| 342 | + ffmpeg_args, |
| 343 | + pass1_omits=None, |
| 344 | + pass1_extras=None, |
| 345 | + overwrite=None, |
| 346 | + stdin=None, |
| 347 | + **other_run_kwargs, |
| 348 | +): |
| 349 | + """run FFmpeg subprocess with standard pipes with a single transaction twice for 2-pass encoding |
| 350 | +
|
| 351 | + :param ffmpeg_args: FFmpeg argument options |
| 352 | + :type ffmpeg_args: dict |
| 353 | + :param pass1_omits: per-file list of output arguments to ignore in pass 1. If not applicable to every |
| 354 | + output file, use a nested dict with int keys to specify which output, |
| 355 | + defaults to None (remove 'c:a' or 'acodec'). |
| 356 | + :type pass1_omits: seq(seq(str)) or dict(int:seq(str)) optional |
| 357 | + :param pass1_extras: per-file list of additional output arguments to include in pass 1. If it does |
| 358 | + not apply to every output files, use a nested dict with int keys to specify |
| 359 | + which output, defaults to None (add 'an' if `pass1_omits` also None) |
| 360 | + :type pass1_extras: seq(dict(str)) or dict(int:dict(str)), optional |
| 361 | + :param hide_banner: False to output ffmpeg banner in stderr, defaults to True |
| 362 | + :type hide_banner: bool, optional |
| 363 | + :param progress: progress callback function, defaults to None. This function |
| 364 | + takes two arguments: |
| 365 | +
|
| 366 | + progress(data:dict, done:bool) -> None |
| 367 | +
|
| 368 | + :type progress: callable object, optional |
| 369 | + :param overwrite: True to overwrite if output url exists, defaults to None |
| 370 | + (auto-select) |
| 371 | + :type overwrite: bool, optional |
| 372 | + :param capture_log: True to capture log messages on stderr, False to send |
| 373 | + logs to console, defaults to None (no show/capture) |
| 374 | + :type capture_log: bool, optional |
| 375 | + :param stdin: source file object, defaults to None |
| 376 | + :type stdin: readable file-like object, optional |
| 377 | + :param stderr: file to log ffmpeg messages, defaults to None |
| 378 | + :type stderr: writable file-like object, optional |
| 379 | + :param input: input data buffer must be given if FFmpeg is configured to receive |
| 380 | + data stream from Python. It must be bytes convertible to bytes. |
| 381 | + :type input: bytes-convertible object, optional |
| 382 | + :param \\**other_popen_kwargs: other keyword arguments of :py:class:`Popen`, defaults to {} |
| 383 | + :type \\**other_popen_kwargs: dict, optional |
| 384 | + :rparam: completed process |
| 385 | + :rtype: subprocess.CompleteProcess |
| 386 | + """ |
| 387 | + |
| 388 | + # TODO allow multiple stream 2-pass encoding |
| 389 | + # TODO add additional arguments to specify which output file |
| 390 | + # TODO add additional arguments to control which output option to be added or dropped during 1st pass |
| 391 | + |
| 392 | + from_stream = stdin is not None |
| 393 | + if from_stream: |
| 394 | + try: |
| 395 | + assert stdin.seekable() |
| 396 | + except: |
| 397 | + raise ValueError("stdin must be seekable") |
| 398 | + |
| 399 | + ffmpeg_args["outputs"] = list(ffmpeg_args["outputs"]) |
| 400 | + |
| 401 | + # ref: https://trac.ffmpeg.org/wiki/Encode/H.264#twopass |
| 402 | + pass1_args = deepcopy(ffmpeg_args) |
| 403 | + |
| 404 | + def mod_pass1_outopts(i, opts): |
| 405 | + opts = opts or {} |
| 406 | + opts["f"] = "null" |
| 407 | + opts["pass"] = 1 |
| 408 | + |
| 409 | + def omit_opt(k): |
| 410 | + try: |
| 411 | + del opts[k] |
| 412 | + except: |
| 413 | + pass |
| 414 | + |
| 415 | + if pass1_omits is None: |
| 416 | + omit_opt("c:a") |
| 417 | + omit_opt("acodec") |
| 418 | + else: |
| 419 | + try: |
| 420 | + for k in pass1_omits[i]: |
| 421 | + omit_opt(k) |
| 422 | + except: |
| 423 | + pass |
| 424 | + |
| 425 | + if pass1_extras is not None: |
| 426 | + try: |
| 427 | + for k, v in pass1_extras.items(): |
| 428 | + opts[k] = v |
| 429 | + except: |
| 430 | + pass |
| 431 | + elif pass1_omits is None: |
| 432 | + opts["an"] = None |
| 433 | + |
| 434 | + return None, opts |
| 435 | + |
| 436 | + pass1_args["outputs"] = [ |
| 437 | + mod_pass1_outopts(i, o[1]) for i, o in enumerate(pass1_args["outputs"]) |
| 438 | + ] |
| 439 | + pass1_opts = pass1_args["global_options"] = pass1_args["global_options"] or {} |
| 440 | + pass1_opts["y"] = None |
| 441 | + try: |
| 442 | + del pass1_opts["n"] |
| 443 | + except: |
| 444 | + pass |
| 445 | + |
| 446 | + def mod_pass2_outopts(url, opts): |
| 447 | + try: |
| 448 | + opts["pass"] = 2 |
| 449 | + return url, opts |
| 450 | + except: |
| 451 | + return (url, {"pass": 2}) |
| 452 | + |
| 453 | + ffmpeg_args["outputs"] = [mod_pass2_outopts(*o) for o in ffmpeg_args["outputs"]] |
| 454 | + |
| 455 | + with TemporaryDirectory() as tmpdir: |
| 456 | + if "passlogfile" not in ffmpeg_args["outputs"][0][1]: |
| 457 | + ffmpeg_args["outputs"][0][1]["passlogfile"] = pass1_args["outputs"][0][1][ |
| 458 | + "passlogfile" |
| 459 | + ] = path.join(tmpdir, "ffmpeg2pass") |
| 460 | + |
| 461 | + if stdin is not None: |
| 462 | + pos = stdin.tell() |
| 463 | + |
| 464 | + run(pass1_args, **other_run_kwargs) |
| 465 | + |
| 466 | + if stdin is not None: |
| 467 | + stdin.seek(pos) |
| 468 | + |
| 469 | + ret = run(ffmpeg_args, overwrite=overwrite, **other_run_kwargs) |
| 470 | + |
| 471 | + # split log lines |
| 472 | + if ret.stderr is not None: |
| 473 | + ret.stderr = ret.stderr.decode("utf-8") |
| 474 | + |
| 475 | + return ret |
0 commit comments