Skip to content

Instantly share code, notes, and snippets.

@nanaze
Last active December 12, 2015 09:09
Show Gist options
  • Save nanaze/4749750 to your computer and use it in GitHub Desktop.
Save nanaze/4749750 to your computer and use it in GitHub Desktop.
convert video files to mp4
import os
import logging
import subprocess
import tempfile
import shutil
def convert(src, dest):
_, temp_dest = tempfile.mkstemp(suffix='.mp4')
assert os.path.exists(src), src + ' does not exist'
#
#
subprocess.check_call(['HandbrakeCLI', '-i', src, '-o', temp_dest])
shutil.move(temp_dest, dest)
def main():
logging.basicConfig(level=logging.DEBUG)
for filename in os.listdir('.'):
root, ext = os.path.splitext(filename)
if ext in ['.mov', '.avi']:
dest = (root + '.mp4')
if os.path.exists(dest):
logging.info('File exists, skipping. %s', dest)
else:
logging.info('Converting file %s to %s', filename, dest)
convert(filename, dest)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment