Last active
March 15, 2016 00:32
-
-
Save SeijiEmery/3ea71a6227fbb47470ae to your computer and use it in GitHub Desktop.
mp4 utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some barebones python scripts I wrote for converting mp4 files (just wraps ffmpeg). | |
– searches the current directory for files (there is no cli interface) | |
– skips files that have already been converted (or a file with the same name and | |
target file extension already exists - eg. foo.avi / foo.mp4 | |
– input file formats can be changed by setting src_formats | |
– runs in parallel using python's awesome multiprocessing library. (see NUM_THREADS) | |
requires ffmpeg / avconv, so install those using your favorite package manager before running. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
from multiprocessing import Pool | |
NUM_THREADS = 8 | |
cur_dir = '.' | |
src_formats = [ '.mp4' ] | |
dst_format = '.mp3' | |
conv_cmd = 'avconv -i "{0}" "{1}"' | |
not_hidden = lambda s: not s.startswith('.') | |
files = filter(not_hidden, os.listdir(cur_dir)) | |
class FileObj(object): | |
def __init__ (self,path): | |
self.name, self.ext = os.path.splitext(path) | |
def __cmp__(self,other): | |
return cmp(self.name, other.name) | |
files = map(FileObj, files) | |
matching_ = lambda exts: lambda f: f.ext in exts | |
matching = lambda exts: matching_(set(exts)) | |
input_files = set(filter(matching(src_formats), files)) | |
output_files = set(filter(matching([ dst_format ]), files)) | |
dupl_files = output_files & input_files | |
if dupl_files: | |
print("Skipping %d file(s): "%len(dupl_files)) | |
for file_ in dupl_files: | |
print('-- %s.%s/.%s'%(file_.name, file_.ext, dst_format)) | |
def run(cmd): | |
print("running '%s'"%cmd) | |
os.system(cmd) | |
files_to_conv = input_files - output_files | |
if files_to_conv: | |
print('Converting %d file(s)'%len(files_to_conv)) | |
join_ext = lambda fname, fext: '%s%s'%(fname, fext) | |
def process_each(file_): | |
run(conv_cmd.format( | |
join_ext(file_.name, file_.ext), | |
join_ext(file_.name, dst_format))) | |
p = Pool(NUM_THREADS) | |
p.map(process_each, files_to_conv) | |
print("Finished converting %d files"%len(files_to_conv)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
from multiprocessing import Pool | |
NUM_THREADS = 8 | |
''' flv/avi/ts/etc to mp4 converter (must have ffmpeg installed!) ''' | |
cur_dir = '.' | |
src_formats = [ '.flv', '.avi', '.ts', '.m4v', '.mkv', '.mov' ] | |
dst_format = '.mp4' | |
conv_cmd = 'ffmpeg -i "{0}" -c copy -copyts "{1}"' | |
not_hidden = lambda s: not s.startswith('.') | |
files = filter(not_hidden, os.listdir(cur_dir)) | |
class FileObj(object): | |
def __init__ (self,path): | |
self.name, self.ext = os.path.splitext(path) | |
def __cmp__(self,other): | |
return cmp(self.name, other.name) | |
files = map(FileObj, files) | |
matching_ = lambda exts: lambda f: f.ext in exts | |
matching = lambda exts: matching_(set(exts)) | |
video_files = set(filter(matching(src_formats), files)) | |
mp4_files = set(filter(matching([ dst_format ]), files)) | |
dupl_files = mp4_files & video_files | |
if dupl_files: | |
print("Skipping %d file(s): "%len(dupl_files)) | |
for file_ in dupl_files: | |
print('-- %s.%s/.%s'%(file_.name, file_.ext, dst_format)) | |
def run(cmd): | |
print("running '%s'"%cmd) | |
os.system(cmd) | |
files_to_conv = video_files - mp4_files | |
if files_to_conv: | |
print('Converting %d file(s)'%len(files_to_conv)) | |
join_ext = lambda fname, fext: '%s%s'%(fname, fext) | |
def process_each(file_): | |
run(conv_cmd.format( | |
join_ext(file_.name, file_.ext), | |
join_ext(file_.name, dst_format))) | |
p = Pool(NUM_THREADS) | |
p.map(process_each, files_to_conv) | |
print("Finished converting %d files"%len(files_to_conv)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment