#!/usr/bin/python2 #---------------------------------------------------- # Author: Florian "Bluewind" Pritz # # Licensed under WTFPL v2 # (see COPYING for full license text) # #---------------------------------------------------- # Converts a file to an MP4 which can be played in a flash movie # Video: H264 # Audio: MPEG-4 AAC #---------------------------------------------------- '''"usage: %prog [options] ''' __version__ ='0.2' __desc__ = 'Use ++ before any argument to pass it directly to ffmpeg.(i.e. ++-r ++25)' import sys import os import subprocess from optparse import OptionParser def main(): usage = "usage: %prog [options] " p = OptionParser(version=__version__, usage=__doc__, description=__desc__) p.add_option("-s", "--size", dest="size", default=False, help="use instead of input dimensions (e.g. 123x123)", metavar="") p.add_option("--vb", dest="vbitrate", default="400000", help="change the video bitrate", metavar="") p.add_option("--ab", dest="abitrate", default="160000", help="change the audio bitrate", metavar="") p.add_option("-m", "--map", action="append", dest="maps", default=None, help="change the mappings", metavar="") p.add_option("--nd", action="store_false", dest="deinterlace", default=True, help="don't deinterlace the video") (options, args) = p.parse_args() if len(args) == 0: p.print_help() sys.exit() ffmpeg_args = [] if options.size: ffmpeg_args.append("-s") ffmpeg_args.append(options.size) if options.vbitrate: ffmpeg_args.append("-b") ffmpeg_args.append(options.vbitrate) if options.abitrate: ffmpeg_args.append("-ab") ffmpeg_args.append(options.abitrate) if options.deinterlace: ffmpeg_args.append("-deinterlace") if options.maps: for cur_map in options.maps: ffmpeg_args.append("-map") ffmpeg_args.append(cur_map) for name in args: if name.startswith('++'): ffmpeg_args.append(name[2:]) for name in args: if name.startswith('++'): continue p1 = subprocess.Popen(["echo", "-n", name], stdout=subprocess.PIPE) p2 = subprocess.Popen(["sed", "s/\(.*\)\..*/\\1/"], stdin=p1.stdout, stdout=subprocess.PIPE) name_mp4 = "tmp_" + p2.communicate()[0] + ".mp4" for encpass in ["1", "2"]: if encpass == "1": filename = "/dev/null" ffmpeg_args.append('-vpre') ffmpeg_args.append('medium_firstpass') else: filename = name_mp4 ffmpeg_args.append('-vpre') ffmpeg_args.append('medium') subprocess.Popen(merge( [["ffmpeg", "-i", name, "-vcodec", "libx264", # "-r", "25", # "-g", "250", "-keyint_min", "25", # "-vpre", "hq", # "-coder", "ac", "-me_range", "16", # "-subq", "5", "-sc_threshold", "40", "-acodec", "libfaac", # "-ar", "44100", # "-cmp", "+chroma", "-partitions", "+parti4x4+partp8x8+partb8x8", # "-i_qfactor", "0.71", "-b_strategy", "1", "-threads", "0", "-pass", encpass, "-f", "mp4", # "-crf", "30", "-y"], ffmpeg_args, [filename]] )).communicate()[0] subprocess.Popen(["qt-faststart", name_mp4, "done_"+name_mp4]).communicate()[0] def merge(seq): merged = [] for s in seq: for x in s: merged.append(x) return merged def file_exists(filename, options): if os.path.exists(filename) and not options.force: sys.stderr.write('Target "'+filename+'" already exists. Use --force to overwrite.\n') sys.exit(1) if __name__ == '__main__': main()