Created
September 24, 2019 02:32
-
-
Save bsidhom/81896505268e4b1c5b6c5b2d92f0ab84 to your computer and use it in GitHub Desktop.
Concatenate MP4s in the order provided using ffmpeg
This file contains hidden or 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 python3 | |
import argparse | |
import io | |
import os.path | |
import re | |
import subprocess | |
import tempfile | |
def merge_videos(inputs, output): | |
with tempfile.NamedTemporaryFile(mode="w+", delete=False) as temp: | |
for f in inputs: | |
path = os.path.abspath(f) | |
# Escape single quotes: https://ffmpeg.org/ffmpeg-formats.html#Examples | |
path = re.sub(r"'", r"'\''", path) | |
print("file '{}'".format(path), file=temp) | |
try: | |
subprocess.run(["ffmpeg", "-f", "concat", "-safe", "0", "-i", temp.name, | |
"-c", "copy", output], check=True) | |
finally: | |
os.remove(temp.name) | |
def main(): | |
parser = argparse.ArgumentParser(description="Concatenates MP4 files") | |
parser.add_argument("--output", required=True) | |
parser.add_argument("inputs", nargs="+") | |
args = parser.parse_args() | |
merge_videos(args.inputs, args.output) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment