Skip to content

Instantly share code, notes, and snippets.

/convert.py Secret

Created July 31, 2016 11:43
Show Gist options
  • Save anonymous/30643e7f2a2558da1e2a3ad4e3d2cc97 to your computer and use it in GitHub Desktop.
Save anonymous/30643e7f2a2558da1e2a3ad4e3d2cc97 to your computer and use it in GitHub Desktop.
#! python3
"""Video processing script.
Call repair.py for all .flv videos in working directory. Three months of
processing later, concatenate videos in working directory by name.
(inb4: traceback at the middle of the night.)
"""
import itertools
import os
import subprocess
import sys
workdir = os.path.dirname(__file__)
# Convert videos to mp4.
files = [os.path.abspath(f) for f in os.listdir(workdir)]
vids = [f for f in files if os.path.splitext(f)[1] == ".flv"]
for vid in vids:
script = os.path.abspath("repair.py")
subprocess.call([script, vid], shell=True)
# Concatenate output by weeks.
files = [os.path.abspath(f) for f in os.listdir(workdir)]
vids = [f for f in files if os.path.splitext(f)[1] == ".mp4"]
def sorter(path):
"""Sort lectures ans critiques vids by week."""
path = os.path.basename(path)
return path[:11] if "Critiques" in path else path[0]
vids = sorted(vids, key=sorter)
groups = itertools.groupby(vids, sorter)
for group, vids in groups:
vids = list(vids)
# Generate a list file containing all videos to concatenate.
listfile = os.path.join(workdir, group)
with open(listfile, "w") as f:
for vid in vids:
line = "file '{}'".format(vid)
print(line, file=f)
output = group + ".mp4"
cexec = [
"ffmpeg",
"-f", "concat",
"-i", listfile,
"-c", "copy",
output
]
subprocess.call(cexec)
os.remove(listfile)
# Finish script and wait when I wake up.
os.system("pause")
#! 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment