Created
May 6, 2020 07:27
-
-
Save alchemyst/2017206ff89d41c163cb7d5f2684c281 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import subprocess | |
import pathlib | |
parser = argparse.ArgumentParser("Concatenate and encode movies") | |
parser.add_argument("infiles", nargs='+', | |
type=argparse.FileType('r'), | |
help='Files to encode') | |
parser.add_argument("-o", "--outfile", default=None) | |
parser.add_argument('--scalewidth', type=int, default=800) | |
parser.add_argument('--scaleheight', type=int, default=600) | |
args = parser.parse_args() | |
# ffmpeg | |
# -i CPB410\ 2019-04-10.mov | |
# -i CPB410\ 2019-04-10b.mov | |
# -filter_complex | |
# "[0:v:0]scale=800:600,setsar=1:1[v0]; | |
# [1:v:0]scale=800:600,setsar=1:1[v1]; | |
# [v0][0:a:0][v1][1:a:0]concat=n=2:v=1:a=1[outv][au]; | |
# [au]acompressor=level_in=3:threshold=-21dB:ratio=9:attack=200:release=1000[outa]" | |
# -map "[outv]" | |
# -map "[outa]" | |
# CPB410\ 2019-04-10.mp4 | |
# [0:v:0]scale=800:600,setsar=1:1[v0]; | |
# [1:v:0]scale=800:600,setsar=1:1[v1]; | |
# [v0][0:a:0][v1][1:a:0]concat=n=2:v=1:a=1[outv][au]; | |
# [au]acompressor=level_in=3:threshold=-21dB:ratio=9:attack=200:release=1000[outa] | |
def notify(s): | |
subprocess.call(["notify", s]) | |
audiofilter = "acompressor=level_in=3:threshold=-21dB:ratio=9:attack=200:release=1000,loudnorm" | |
command = "ffmpeg" | |
if args.outfile is None: | |
args.outfile = pathlib.Path(args.infiles[0].name).with_suffix('.mp4') | |
encodeargs = [] | |
filterchain = [] | |
concat = '' | |
for i, infile in enumerate(args.infiles): | |
encodeargs += ["-i", infile.name] | |
filterchain.append(f"[{i}:v:0]scale={args.scalewidth}:{args.scaleheight},setsar=1:1[v{i}]") | |
concat += f'[v{i}][{i}:a:0]' | |
concat += f'concat=n={len(args.infiles)}:v=1:a=1[outv][au]' | |
filterchain.append(concat) | |
filterchain.append(f'[au]{audiofilter}[outa]') | |
encodeargs.append("-filter_complex") | |
encodeargs.append(";".join(filterchain)) | |
encodeargs += ['-map', '[outv]', '-map', '[outa]'] | |
encodeargs += [args.outfile] | |
print(encodeargs) | |
returncode = subprocess.call([command, *encodeargs]) | |
if returncode: | |
notify(f"Encoding failed with code {returncode}") | |
else: | |
notify("Encoding success") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment