Skip to content

Instantly share code, notes, and snippets.

@alchem0x2A
Last active December 10, 2018 11:15
Show Gist options
  • Save alchem0x2A/66422870f4e49e272f09b70d647f38fa to your computer and use it in GitHub Desktop.
Save alchem0x2A/66422870f4e49e272f09b70d647f38fa to your computer and use it in GitHub Desktop.
Convert all video files under current folder to mp4
import os, os.path
import subprocess
def convert(in_name, out_name, delete=True):
prog = "ffmpeg"
params = ["-i", "{}".format(in_name),
"-pix_fmt", "yuv420p",
"-vcodec", "libx264",
"{}".format(out_name)]
res = subprocess.call([prog, *params])
if delete:
os.remove(in_name)
return res
for line in os.walk("./"):
base, sub_dir, files = line
if (len(files) > 0): # True files, not dirs
for f in files:
f_name, ext = os.path.splitext(f)
full_path = os.path.join(base, f)
if ext in (".mp4", ".webm", ".ogg"): # Can use preview
print("Omitted: {}".format(full_path))
continue
elif ext in (".rm", ".rmvb", ".wmv",
".mkv", ".mpg", ".mov",
".flv", "avi"):
convert_path = os.path.join(base, "{}.mp4".format(f_name))
res = convert(in_name=full_path, out_name=convert_path,
delete=True)
print("Convert: {} -> {} with code {}".format(full_path,
convert_path,
res))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment