Skip to content

Instantly share code, notes, and snippets.

@kongakong
Last active April 25, 2021 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kongakong/df4863ae134aeb6a954e00143ac8d89e to your computer and use it in GitHub Desktop.
Save kongakong/df4863ae134aeb6a954e00143ac8d89e to your computer and use it in GitHub Desktop.
Concurrent conversion of video files with ffmpeg
#!/usr/bin/env python3
import os
import sys
import subprocess
import datetime
import time
from multiprocessing import Pool
def convert_to_iso_mp4(params):
srcfile, outfile, destdir = params
outpath = os.path.join(destdir, outfile)
if os.path.exists(outpath):
print(f"The file {outpath} is already processed")
return
cmd = f"ffmpeg -i {srcfile} -vcodec libx264 -profile:v main -level 3.1 -preset slower -crf 18 -x264-params ref=4 -acodec copy -movflags +faststart {outpath}"
subprocess.check_call(cmd.split())
d = srcfile.split("_")[1]
do = datetime.datetime.strptime(d, "%Y%m%d%H%M%S")
dt = time.mktime(do.timetuple())
os.utime(outpath, (dt, dt))
def main():
filelist = sys.argv[1]
destdir = sys.argv[2]
print("Working on", filelist)
print("Will save to", destdir)
jobs = []
for l in open(filelist, "r"):
# Ignore commented lines
if not l.startswith("#"):
infile = l.strip()
outfile = f"ios_{infile}"
jobs.append(
(
infile,
outfile,
destdir,
)
)
with Pool(6) as p:
p.map(convert_to_iso_mp4, jobs)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment