Skip to content

Instantly share code, notes, and snippets.

@kracekumar
Created October 22, 2012 05:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kracekumar/3929886 to your computer and use it in GitHub Desktop.
Save kracekumar/3929886 to your computer and use it in GitHub Desktop.
convert mts files to mp4
#! /usr/bin/env python
import sys
import os
#import gevent
def convert_to_mp4(path, new_filename, old_filename):
threads = 4
os.chdir(path)
try:
os.system("ffmpeg -i %s -threads %d -f mp4 %s" %(old_filename, threads, new_filename))
print("ffmpeg completed converting for %s" % old_filename)
if os.path.exists(new_filename):
os.remove(old_filename)
except:
raise
print("Something went wrong")
if __name__ == "__main__":
path = sys.argv[1]
if path:
all_files = os.listdir(path)
tasks = [convert_to_mp4(path, filename.split('.')[0]+'.mp4', filename) for filename in all_files if filename.endswith('.MTS')]
#gevent.joinall(tasks)
@jace
Copy link

jace commented Oct 22, 2012

Let's see if I can write you the bash equivalent:

for FILE in *.MTS; do if [ ! -f ${FILE%%.MTS} ]; then ffmpeg -i $FILE -threads 4 -f mp4 ${FILE%%.MTS}.mp4; fi; done

@jace
Copy link

jace commented Oct 22, 2012

Typo. Missed a .mp4:

for FILE in *.MTS; do if [ ! -f ${FILE%%.MTS}.mp4 ]; then ffmpeg -i $FILE -threads 4 -f mp4 ${FILE%%.MTS}.mp4; fi; done

@abdulmuneer
Copy link

os.system is deprecated. Repplaced by subprocess module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment