summaryrefslogtreecommitdiffstats
path: root/convert2mp4
blob: fb285c98132c652876759397f807b29576c8ca9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python
#----------------------------------------------------
# Author:       Florian "Bluewind" Pritz <flo@xssn.at>
#
# 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] <files>'''
__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] <files>"
  p = OptionParser(version=__version__, usage=__doc__, description=__desc__)
  p.add_option("-s", "--size", dest="size", default=False,
      help="use <dimension> instead of input dimensions (e.g. 123x123)", metavar="<dimension>")
  p.add_option("--vb", dest="vbitrate", default="400000",
      help="change the video bitrate", metavar="<bitrate>")
  p.add_option("--ab", dest="abitrate", default="160000",
      help="change the audio bitrate", metavar="<bitrate>")
  p.add_option("-m", "--map", action="append", dest="maps", default=None,
      help="change the mappings", metavar="<input_stream_id:sync_stream_id>")
  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 = p2.communicate()[0] + ".mp4"
        for encpass in ["1", "2"]:
            if encpass == "1":
                filename = "/dev/null"
            else:
                filename = name_mp4
            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()