Skip to content

Instantly share code, notes, and snippets.

/repair.py Secret

Created June 18, 2015 17:36
Show Gist options
  • Save anonymous/db5291375774c7ea0a19 to your computer and use it in GitHub Desktop.
Save anonymous/db5291375774c7ea0a19 to your computer and use it in GitHub Desktop.
Video processing script
#! python3
"""Video processing script.
Video is twice as slow as audio. Here we extract sequence at 12 FPS,
then encode a new one with 24 FPS using an original audio.
"""
import os
import shutil
import subprocess
import sys
try:
vpath = os.path.abspath(sys.argv[1])
except IndexError:
scriptname = os.path.basename(__file__)
raise Exception("Usage\n\t{} input.flv".format(scriptname))
try:
workdir, _ = os.path.splitext(vpath)
os.mkdir(workdir)
except FileExistsError:
raise Exception("Unable to create a fresh folder at " + workdir)
# Sequence extraction command.
spath = os.path.join(workdir, r"%05d.jpg")
sexec = [
"ffmpeg", # ffmpeg executable.
"-i", vpath, # Bad video input.
"-r", "12", # Assumed framerate.
"-q:v", "1", # Explode in maximum quality.
spath # Sequence output.
]
# Audio extraction command.
apath = os.path.join(workdir, "audio.aac")
aexec = [
"ffmpeg",
"-i", vpath,
"-c:a", "copy", # Audio codec (no encode).
apath # Audio output.
]
# Encode command.
epath = workdir + ".mp4"
eexec = [
"ffmpeg",
"-framerate", "24", # Desired framerate.
"-i", spath, # Sequence input.
"-i", apath, # Audio input.
"-c:v", "libx264", # Video codec.
"-c:a", "aac", # Audio codec (aac).
"-strict", "experimental", # Enabling the aac codec.
epath # Ecoded mp4 output.
]
# Run commands.
subprocess.call(sexec)
subprocess.call(aexec)
subprocess.call(eexec)
# Cleanup and finish.
shutil.rmtree(workdir)
os.system("pause")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment