summaryrefslogtreecommitdiffstats
path: root/convert2mp4
blob: 903557211420ec49caeaf43ebd90ca054001326b (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
#!/usr/bin/python
#----------------------------------------------------
# Version:    0.2.0
# Author:   Florian "Bluewind" Pritz <f-p@gmx.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
#----------------------------------------------------

import sys
import os
import subprocess
from optparse import OptionParser

def main():
  usage = "usage: %prog [options] <files>"
  p = OptionParser(usage)
  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("-e", "--extra", action="append", dest="extras", default=None,
      help="pass extra options to ffmpeg", nargs=2, metavar="<ffmpeg switch> <argument>")
  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)
  if options.extras:
    for switch, arg in options.extras:
      ffmpeg_args.append(switch)
      ffmpeg_args.append(arg)


  for name in args:
        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"
        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",
#            "-crf", "30",
            "-y"],
            ffmpeg_args, [name_mp4]]
            )).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()