Last active
April 25, 2021 05:52
-
-
Save kongakong/df4863ae134aeb6a954e00143ac8d89e to your computer and use it in GitHub Desktop.
Concurrent conversion of video files with ffmpeg
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 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