1 import re
2 import logging
3 log = logging.getLogger("modules.AVConverter")
4
5 import conduit
6 import conduit.utils as Utils
7 import conduit.utils.CommandLineConverter as CommandLineConverter
8 import conduit.TypeConverter as TypeConverter
9 import conduit.datatypes.File as File
10 import conduit.datatypes.Audio as Audio
11 import conduit.datatypes.Video as Video
12
13 if Utils.program_installed("ffmpeg"):
14 MODULES = {
15 "AudioVideoConverter" : { "type": "converter" }
16 }
17 else:
18 MODULES = {}
19
25
27 kwargs['in_file'] = '"%s"'
28 kwargs['out_file'] = '"%s"'
29
30 command = "ffmpeg -i %(in_file)s "
31
32 if kwargs.get('vcodec', None): command += "-vcodec %(vcodec)s "
33 if kwargs.get('vbitrate', None): command += "-b %(vbitrate)s "
34 if kwargs.get('fps', None): command += "-r %(fps)s "
35 if kwargs.get('vtag', None): command += "-vtag %(vtag)s "
36 if kwargs.get('width', None) and kwargs.get('height', None):
37 command += "-s %(width)sx%(height)s "
38
39 if kwargs.get('acodec', None): command += "-acodec %(acodec)s "
40 if kwargs.get('arate', None): command += "-ar %(arate)s "
41
42 if kwargs.get('achannels', None): command += "-ac %(achannels)s "
43
44 if kwargs.get('format', None): command += "-f %(format)s "
45 command += "-y %(out_file)s"
46
47 self.command = command % kwargs
48
50 return float(val)/self.duration*100.0
51
54
59
61 kwargs['in_file'] = '"%s"'
62 kwargs['out_file'] = '"%s"'
63
64 command = "mencoder %(in_file)s -o %(out_file)s "
65
66 if kwargs.get('arate', None): command += "-srate %(arate)s "
67 if kwargs.get('achannels', None): command += "-channels %(achannels) "
68
69 command += "-oac lavc "
70 if kwargs.has_key('acodec') and kwargs.has_key('abitrate'):
71 command += "-lavcopts acodec=%(acodec)s:abitrate=%(abitrate)s "
72 if kwargs.get('achannels', None):
73 command += "-af volnorm,channels=%(achannels) "
74 else:
75 command += "-af volnorm "
76
77 command += "-ovc lavc "
78 if kwargs.has_key('vcodec') and kwargs.has_key('vbitrate'):
79 command += "-ovc lavc -lavcopts vcodec=%(vcodec)s:vbitrate=%(vbitrate)s "
80 if kwargs.get('width', None) and kwargs.get('height', None):
81 command += "-vf-add scale=%(width)s:%(height)s "
82 if kwargs.get('fps', None): command += "-ofps %(fps)s "
83 if kwargs.get('vtag', None): command += "-ffourcc %(vtag)s "
84
85 self.command = command % kwargs
86
89
92
94
95
96
97
98
99
100 VIDEO_INSPECT_COMMAND = 'ffmpeg -an -y -t 0:0:0.001 -i "%s" -f image2 "%s" 2>&1'
101 AUDIO_INSPECT_COMMAND = 'ffmpeg -fs 1 -y -i "%s" -f wav "%s" 2>&1'
102
110
112 mimetype = video.get_mimetype()
113 if not Video.mimetype_is_video(mimetype):
114 log.debug("File %s is not video type: %s" % (video,mimetype))
115 return None
116 input_file = video.get_local_uri()
117
118
119 c = CommandLineConverter.CommandLineConverter()
120 c.build_command(AudioVideoConverter.VIDEO_INSPECT_COMMAND)
121 ok,output = c.convert(input_file,"/dev/null",save_output=True)
122
123 if not ok:
124 log.debug("Error getting video information\n%s" % output)
125 return None
126
127
128 pat = re.compile(r'Input.*?Duration: ([\d:]*\.*\d*).*?Stream #\d\.\d: Video:.*?(\d+)x(\d+)',re.DOTALL)
129 try:
130 duration_string,w,h = re.search(pat,output).groups()
131
132 ho,m,s = duration_string