Skip to content

Instantly share code, notes, and snippets.

@tokejepsen
Last active March 11, 2021 09:32
Show Gist options
  • Save tokejepsen/cba371f09584fc062a204031101e0d6b to your computer and use it in GitHub Desktop.
Save tokejepsen/cba371f09584fc062a204031101e0d6b to your computer and use it in GitHub Desktop.
ffmpeg compression
import os
import sys
import subprocess
def transcode_movies(path):
movie_files = []
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith("_review.mov"):
continue
if file.endswith("_proxy.mov"):
continue
if file.endswith(".mov"):
movie_files.append(os.path.join(root, file))
cmds = {}
cmds["reviews"] = "ffmpeg -y -i {} -crf 18 -pix_fmt yuv420p {}_review.mp4"
cmds["proxies"] = (
"ffmpeg -y -i {} -vf scale=960:-1 -c:v prores -profile:v 0 "
"-pix_fmt yuv422p10le {}_proxy.mov"
)
for source_file in movie_files:
for name, cmd in cmds.items():
target_file = os.path.join(
os.path.dirname(source_file),
name,
os.path.basename(source_file).replace(".mov", "")
)
if not os.path.exists(os.path.dirname(target_file)):
os.makedirs(os.path.dirname(target_file))
print("Processing {}".format(cmd.format(source_file, target_file)))
p = subprocess.Popen(
cmd.format(source_file, target_file),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
stdin=subprocess.PIPE,
cwd=os.path.dirname(source_file)
)
output = p.communicate()[0]
if p.returncode != 0:
raise ValueError(output)
transcode_movies(os.path.join(os.path.expanduser("~"), "Desktop", "movs"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment