Debian Codegraph @

src/devedeng/avconv.py

raw
1 #!/usr/bin/env python3
2
3 # Copyright 2014 (C) Raster Software Vigo (Sergio Costas)
4 #
5 # This file is part of DeVeDe-NG
6 #
7 # DeVeDe-NG is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 3
9 # as published by the Free Software Foundation.
10 #
11 # DeVeDe-NG is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>
18
19 import subprocess
20 import os
21 import devedeng.configuration_data
22 import devedeng.avbase
23 import devedeng.mux_dvd_menu
24 import devedeng.project
25
26 class avconv(devedeng.avbase.avbase):
27
28 supports_analize = False
29 supports_play = False
30 supports_convert = True
31 supports_menu = True
32 supports_mkiso = False
33 supports_burn = False
34 display_name = "AVCONV"
35 disc_types = []
36
37 @staticmethod
38 def check_is_installed():
39 try:
40 handle = subprocess.Popen(
41 ["avconv", "-codecs"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
42 (stdout, stderr) = handle.communicate()
43 if 0 == handle.wait():
44 mp2 = False
45 mp3 = False
46 ac3 = False
47 mpeg1 = False
48 mpeg2 = False
49 divx = False
50 h264 = False
51 hevc = False
52 for line in stdout.decode("latin-1").split("\n"):
53 parts = line.strip().split(" ")
54 if len(parts) < 2:
55 continue
56 if len(parts[0]) != 6:
57 continue
58 capabilities = parts[0]
59 codec = parts[1]
60
61 if capabilities[1] != 'E':
62 continue
63
64 if (codec == "mpeg1video"):
65 mpeg1 = True
66 continue
67 if (codec == "mpeg2video"):
68 mpeg2 = True
69 continue
70 if (codec == "mp2"):
71 mp2 = True
72 continue
73 if (codec == "mp3"):
74 mp3 = True
75 continue
76 if (codec == "ac3"):
77 ac3 = True
78 continue
79 if (codec == "h264") or (codec == "H264"):
80 h264 = True
81 continue
82 if (codec == "hevc") or (codec == "HEVC"):
83 hevc = True
84 continue
85 if (codec == "mpeg4"):
86 divx = True
87 continue
88
89 if (mpeg1 and mp2):
90 devedeng.avconv.avconv.disc_types.append("vcd")
91 if (mpeg2 and mp2):
92 devedeng.avconv.avconv.disc_types.append("svcd")
93 devedeng.avconv.avconv.disc_types.append("cvd")
94 if (mpeg2 and mp2 and ac3):
95 devedeng.avconv.avconv.disc_types.append("dvd")
96 if (divx and mp3):
97 devedeng.avconv.avconv.disc_types.append("divx")
98 if (h264 and mp3):
99 devedeng.avconv.avconv.disc_types.append("mkv")
100 if (hevc and mp3):
101 devedeng.ffmpeg.ffmpeg.disc_types.append("hevc")
102
103 return True
104 else:
105 return False
106 except:
107 return False
108
109 def __init__(self):
110
111 devedeng.executor.executor.__init__(self)
112 self.config = devedeng.configuration_data.configuration.get_config()
113 self.check_version(["avconv", "-version"])
114
115 def convert_file(self, file_project, output_file, video_length, pass2=False):
116
117 if file_project.two_pass_encoding:
118 if pass2:
119 self.text = _("Converting %(X)s (pass 2)") % {
120 "X": file_project.title_name}
121 else:
122 self.text = _("Converting %(X)s (pass 1)") % {
123 "X": file_project.title_name}
124 # Prepare the converting process for the second pass
125 tmp = devedeng.avconv.avconv()
126 tmp.convert_file(file_project, output_file, video_length, True)
127 # it deppends of this process
128 tmp.add_dependency(self)
129 # add it as a child process of this one
130 self.add_child_process(tmp)
131 else:
132 self.text = _("Converting %(X)s") % {"X": file_project.title_name}
133
134 if (pass2 == False) and (file_project.two_pass_encoding == True):
135 # this is the first pass in a 2-pass codification
136 second_pass = False
137 else:
138 # second_pass is TRUE in the second pass of a 2-pass codification, and also when not doing 2-pass codification
139 # It is used to remove unnecessary steps during the first pass, but
140 # that are needed on the second pass, or when not using 2-pass
141 # codification
142 second_pass = True
143
144 if (video_length == 0):
145 self.final_length = file_project.original_length
146 else:
147 self.final_length = video_length
148 self.command_var = []
149 self.command_var.append("avconv")
150 self.command_var.append("-i")
151 self.command_var.append(file_project.file_name)
152
153 if (file_project.volume != 100) and second_pass:
154 self.command_var.append("-vol")
155 self.command_var.append(
156 str(int((256 * file_project.volume) / 100)))
157
158 if (file_project.audio_delay != 0.0) and (file_project.copy_sound == False) and (file_project.no_reencode_audio_video == False) and second_pass:
159 self.command_var.append("-itsoffset")
160 self.command_var.append(str(file_project.audio_delay))
161
162 self.command_var.append("-i")
163 self.command_var.append(file_project.file_name)
164 self.command_var.append("-map")
165 self.command_var.append("1:" + str(file_project.video_list[0]))
166 if (not file_project.copy_sound) and (not file_project.no_reencode_audio_video):
167 for l in file_project.audio_list:
168 self.command_var.append("-map")
169 self.command_var.append("0:" + str(l))
170
171 if (file_project.no_reencode_audio_video == False) and second_pass:
172 cmd_line = ""
173
174 if file_project.deinterlace == "deinterlace_yadif":
175 cmd_line += "yadif"
176
177 vflip = False
178 hflip = False
179
180 if (file_project.prerotation == 90):
181 if (cmd_line != ""):
182 cmd_line += ",fifo,"
183 cmd_line += "transpose=1"
184 elif (file_project.prerotation == -90) or (file_project.prerotation == 270):
185 if (cmd_line != ""):
186 cmd_line += ",fifo,"
187 cmd_line += "transpose=2"
188 elif (file_project.prerotation == 180):
189 if (cmd_line != ""):
190 cmd_line += ",fifo,"
191 vflip = not vflip
192 hflip = not hflip
193
194 if (file_project.rotation == "rotation_90"):
195 if (cmd_line != ""):
196 cmd_line += ",fifo,"
197 cmd_line += "transpose=1"
198 elif (file_project.rotation == "rotation_270"):
199 if (cmd_line != ""):
200 cmd_line += ",fifo,"
201 cmd_line += "transpose=2"
202 elif (file_project.rotation == "rotation_180"):
203 vflip = not vflip
204 hflip = not hflip
205
206 if (file_project.mirror_vertical):
207 vflip = not vflip
208 if (file_project.mirror_horizontal):
209 hflip = not hflip
210
211 if (vflip):
212 if (cmd_line != ""):
213 cmd_line += ",fifo,"
214 cmd_line += "vflip"
215 if (hflip):
216 if (cmd_line != ""):
217 cmd_line += ",fifo,"
218 cmd_line += "hflip"
219
220 if (file_project.width_midle != file_project.original_width) or (file_project.height_midle != file_project.original_height):
221 if (cmd_line != ""):
222 cmd_line += ",fifo,"
223 x = int((file_project.width_midle -
224 file_project.original_width) / 2)
225 y = int((file_project.height_midle -
226 file_project.original_height) / 2)
227 if (x > 0) or (y > 0):
228 cmd_line += "pad=" + str(file_project.width_midle) + ":" + str(
229 file_project.height_midle) + ":" + str(x) + ":" + str(y) + ":0x000000"
230 else:
231 cmd_line += "crop=" + str(file_project.width_midle) + ":" + str(
232 file_project.height_midle) + ":" + str(x) + ":" + str(y)
233
234 if (file_project.width_final != file_project.width_midle) or (file_project.height_final != file_project.height_midle):
235 if (cmd_line != ""):
236 cmd_line += ",fifo,"
237 if self.major_version < 11:
238 cmd_line += "scale=" + \
239 str(file_project.width_final) + ":" + \
240 str(file_project.height_final)
241 else:
242 cmd_line += "scale=w=" + \
243 str(file_project.width_final) + ":h=" + \
244 str(file_project.height_final)
245
246 if cmd_line != "":
247 self.command_var.append("-vf")
248 self.command_var.append(cmd_line)
249
250 self.command_var.append("-y")
251
252 vcd = False
253
254 maxrate = int(file_project.video_rate_final * 1500)
255 minrate = int(file_project.video_rate_final * 666.66)
256
257 if (self.config.disc_type == "divx"):
258 self.command_var.append("-vcodec")
259 self.command_var.append("mpeg4")
260 self.command_var.append("-acodec")
261 self.command_var.append("libmp3lame")
262 self.command_var.append("-f")
263 self.command_var.append("avi")
264 if not pass2:
265 self.command_var.append("-maxrate:v")
266 self.command_var.append(str(maxrate))
267 self.command_var.append("-minrate:v")
268 if minrate < (devedeng.project.devede_project.divx_min_bps * 1000):
269 minrate = devedeng.project.devede_project.divx_min_bps * 1000
270 self.command_var.append(str(minrate))
271 elif (self.config.disc_type == "mkv") or (self.config.disc_type == "hevc"):
272 self.command_var.append("-vcodec")
273 self.command_var.append("h264" if self.config.disc_type == "mkv" else "hevc")
274 self.command_var.append("-acodec")
275 self.command_var.append("libmp3lame")
276 self.command_var.append("-f")
277 self.command_var.append("matroska")
278 if not pass2:
279 self.command_var.append("-maxrate:v")
280 self.command_var.append(str(maxrate))
281 self.command_var.append("-minrate:v")
282 if minrate < (devedeng.project.devede_project.mkv_min_bps * 1000):
283 minrate = devedeng.project.devede_project.mkv_min_bps * 1000
284 self.command_var.append(str(minrate))
285 elif (self.config.disc_type == "dvd"):
286 if not file_project.no_reencode_audio_video:
287 self.command_var.append("-c:v")
288 self.command_var.append("mpeg2video")
289 if not file_project.copy_sound:
290 if file_project.sound5_1:
291 self.command_var.append("-c:a")
292 self.command_var.append("ac3")
293 else:
294 self.command_var.append("-c:a")
295 if file_project.format_pal:
296 self.command_var.append("ac3")
297 else:
298 self.command_var.append("mp2")
299 self.command_var.append("-f")
300 self.command_var.append("dvd")
301 self.command_var.append("-r")
302 if file_project.format_pal:
303 self.command_var.append("25")
304 else:
305 if (file_project.original_fps == 24):
306 self.command_var.append("24000/1001")
307 else:
308 self.command_var.append("30000/1001")
309 self.command_var.append("-pix_fmt")
310 self.command_var.append("yuv420p")
311 self.command_var.append("-maxrate:v")
312 if maxrate > (devedeng.project.devede_project.dvd_max_bps_base[0] * 1000):
313 maxrate = devedeng.project.devede_project.dvd_max_bps_base[0] * 1000
314 self.command_var.append(str(maxrate))
315 if not pass2:
316 self.command_var.append("-minrate:v")
317 if minrate < (devedeng.project.devede_project.dvd_min_bps * 1000):
318 minrate = devedeng.project.devede_project.dvd_min_bps * 1000
319 self.command_var.append(str(minrate))
320 self.command_var.append("-bufsize")
321 self.command_var.append("1835008")
322 self.command_var.append("-packetsize")
323 self.command_var.append("2048")
324 self.command_var.append("-muxrate")
325 self.command_var.append("10080000")
326 self.command_var.append("-ar")
327 self.command_var.append("48000")
328 elif (self.config.disc_type == "vcd"):
329 vcd = True
330 if not file_project.no_reencode_audio_video:
331 self.command_var.append("-c:v")
332 self.command_var.append("mpeg1video")
333 if not file_project.copy_sound:
334 self.command_var.append("-c:a")
335 self.command_var.append("mp2")
336 self.command_var.append("-b:v")
337 self.command_var.append("1150000")
338 self.command_var.append("-b:a")
339 self.command_var.append("224000")
340 self.command_var.append("-f")
341 self.command_var.append("vcd")
342 self.command_var.append("-r")
343 if file_project.format_pal:
344 self.command_var.append("25")
345 else:
346 if (file_project.original_fps == 24):
347 self.command_var.append("24000/1001")
348 else:
349 self.command_var.append("30000/1001")
350 self.command_var.append("-g")
351 if file_project.format_pal:
352 self.command_var.append("15")
353 else:
354 self.command_var.append("18")
355 self.command_var.append("-s")
356 if file_project.format_pal:
357 self.command_var.append("352x288")
358 else:
359 self.command_var.append("352x240")
360 self.command_var.append("-maxrate:v")
361 self.command_var.append("1150000")
362 self.command_var.append("-minrate:v")
363 self.command_var.append("1150000")
364 self.command_var.append("-bufsize")
365 self.command_var.append("327680")
366 self.command_var.append("-packetsize")
367 self.command_var.append("2324")
368 self.command_var.append("-muxrate")
369 self.command_var.append("1411200")
370 self.command_var.append("-ar")
371 self.command_var.append("44100")
372 elif (self.config.disc_type == "svcd") or (self.config.disc_type == "cvd"):
373 if not file_project.no_reencode_audio_video:
374 self.command_var.append("-c:v")
375 self.command_var.append("mpeg2video")
376 if not file_project.copy_sound:
377 self.command_var.append("-c:a")
378 self.command_var.append("mp2")
379 self.command_var.append("-f")
380 self.command_var.append("svcd")
381 self.command_var.append("-r")
382 if file_project.format_pal:
383 self.command_var.append("25")
384 else:
385 if (file_project.original_fps == 24):
386 self.command_var.append("24000/1001")
387 else:
388 self.command_var.append("30000/1001")
389 self.command_var.append("-g")
390 if file_project.format_pal:
391 self.command_var.append("12")
392 else:
393 self.command_var.append("15")
394 self.command_var.append("-s")
395 if self.config.disc_type == "cvd":
396 if file_project.format_pal:
397 self.command_var.append("352x576")
398 else:
399 self.command_var.append("352x480")
400 else:
401 if file_project.format_pal:
402 self.command_var.append("480x576")
403 else:
404 self.command_var.append("480x480")
405 self.command_var.append("-pix_fmt")
406 self.command_var.append("yuv420p")
407 self.command_var.append("-maxrate:v")
408 if maxrate > (devedeng.project.devede_project.svcd_max_bps_base[0] * 1000):
409 maxrate = devedeng.project.devede_project.svcd_max_bps_base[0] * 1000
410 self.command_var.append(str(maxrate))
411 if not pass2:
412 self.command_var.append("-minrate:v")
413 if minrate < (devedeng.project.devede_project.svcd_min_bps * 1000):
414 minrate = (devedeng.project.devede_project.svcd_min_bps * 1000)
415 self.command_var.append(str(minrate))
416 self.command_var.append("-bufsize")
417 self.command_var.append("1835008")
418 self.command_var.append("-packetsize")
419 self.command_var.append("2324")
420 self.command_var.append("-ar")
421 self.command_var.append("44100")
422 self.command_var.append("-scan_offset")
423 self.command_var.append("1")
424
425 if (not file_project.no_reencode_audio_video):
426 self.command_var.append("-sn") # no subtitles
427
428 if file_project.copy_sound or file_project.no_reencode_audio_video:
429 self.command_var.append("-acodec")
430 self.command_var.append("copy")
431
432 if file_project.no_reencode_audio_video:
433 self.command_var.append("-vcodec")
434 self.command_var.append("copy")
435
436 if (not vcd):
437 if not file_project.format_pal:
438 if (file_project.original_fps == 24) and ((self.config.disc_type == "dvd")):
439 keyintv = 15
440 else:
441 keyintv = 18
442 else:
443 keyintv = 15
444
445 if (not file_project.gop12) and (not(self.config.disc_type == "divx")) and (not(self.config.disc_type == "mkv") and (not(self.config.disc_type == "hevc"))):
446 self.command_var.append("-g")
447 self.command_var.append(str(keyintv))
448
449 if (self.config.disc_type == "divx") or (self.config.disc_type == "mkv") or (self.config.disc_type == "hevc"):
450 self.command_var.append("-g")
451 self.command_var.append("300")
452 elif file_project.gop12 and (not file_project.no_reencode_audio_video):
453 self.command_var.append("-g")
454 self.command_var.append("12")
455
456 self.command_var.append("-bf")
457 self.command_var.append("2")
458 self.command_var.append("-strict")
459 self.command_var.append("1")
460
461 if video_length != 0:
462 self.command_var.append("-t")
463 self.command_var.append(str(video_length))
464
465 self.command_var.append("-ac")
466 if (file_project.sound5_1) and ((self.config.disc_type == "dvd") or (self.config.disc_type == "divx") or (self.config.disc_type == "mkv") or (self.config.disc_type == "hevc")):
467 self.command_var.append("6")
468 else:
469 self.command_var.append("2")
470
471 self.command_var.append("-aspect")
472 self.command_var.append(str(file_project.aspect_ratio_final))
473
474 if self.config.disc_type == "divx":
475 self.command_var.append("-vtag")
476 self.command_var.append("DX50")
477
478 if (file_project.deinterlace == "deinterlace_ffmpeg") and (file_project.no_reencode_audio_video == False) and second_pass:
479 self.command_var.append("-deinterlace")
480
481 if (file_project.no_reencode_audio_video == False) and (vcd == False) and second_pass:
482 self.command_var.append("-s")
483 self.command_var.append(
484 str(file_project.width_final) + "x" + str(file_project.height_final))
485
486 if second_pass:
487 self.command_var.append("-trellis")
488 self.command_var.append("1")
489 self.command_var.append("-mbd")
490 self.command_var.append("2")
491 else:
492 self.command_var.append("-trellis")
493 self.command_var.append("0")
494 self.command_var.append("-mbd")
495 self.command_var.append("0")
496
497 if (vcd == False) and (file_project.no_reencode_audio_video == False):
498 self.command_var.append("-b:a")
499 self.command_var.append(str(self._adjust_audio_bitrate(file_project.audio_rate_final)*1000))
500
501 self.command_var.append("-b:v")
502 self.command_var.append(str(file_project.video_rate_final*1000))
503
504 if file_project.two_pass_encoding == True:
505 self.command_var.append("-passlogfile")
506 self.command_var.append(output_file + ".passlog")
507 self.command_var.append("-pass")
508 if pass2:
509 self.command_var.append("2")
510 else:
511 self.command_var.append("1")
512
513 self.command_var.append(output_file)
514
515 def _adjust_audio_bitrate(self, bitrate):
516 br = [32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384]
517 for a in br:
518 if a >= bitrate:
519 return a
520 return 384
521
522 def create_menu_mpeg(self, n_page, background_music, sound_length, pal, video_rate, audio_rate, output_path, use_mp2, widescreen):
523
524 self.n_page = n_page
525 self.final_length = float(sound_length)
526 self.text = _("Creating menu %(X)d") % {"X": self.n_page}
527
528 self.command_var = []
529 self.command_var.append("avconv")
530
531 self.command_var.append("-loop")
532 self.command_var.append("1")
533
534 self.command_var.append("-f")
535 self.command_var.append("image2")
536 self.command_var.append("-i")
537 self.command_var.append(os.path.join(
538 output_path, "menu_" + str(n_page) + "_bg.png"))
539 self.command_var.append("-i")
540 self.command_var.append(background_music)
541
542 self.command_var.append("-y")
543 self.command_var.append("-target")
544 if pal:
545 self.command_var.append("pal-dvd")
546 else:
547 self.command_var.append("ntsc-dvd")
548 self.command_var.append("-acodec")
549 if use_mp2:
550 self.command_var.append("mp2")
551 if (audio_rate > 384):
552 audio_rate = 384 # max bitrate for mp2
553 else:
554 self.command_var.append("ac3")
555 self.command_var.append("-s")
556 if pal:
557 self.command_var.append("720x576")
558 else:
559 self.command_var.append("720x480")
560 self.command_var.append("-g")
561 self.command_var.append("12")
562 self.command_var.append("-b:v")
563 self.command_var.append(str(video_rate) + "k")
564 self.command_var.append("-b:a")
565 self.command_var.append(str(self._adjust_audio_bitrate(audio_rate)) + "k")
566 self.command_var.append("-aspect")
567 if widescreen:
568 self.command_var.append("16:9")
569 else:
570 self.command_var.append("4:3")
571
572 self.command_var.append("-t")
573 self.command_var.append(str(1 + sound_length))
574
575 movie_path = os.path.join(output_path, "menu_" + str(n_page) + ".mpg")
576 self.command_var.append(movie_path)
577
578 muxer = devedeng.mux_dvd_menu.mux_dvd_menu()
579 final_path = muxer.create_mpg(n_page, output_path, movie_path)
580 # the muxer process depends of the converter process
581 muxer.add_dependency(self)
582 self.add_child_process(muxer)
583
584 return (final_path)
585
586 def process_stdout(self, data):
587
588 return
589
590 def process_stderr(self, data):
591
592 pos = data[0].find("time=")
593 if (pos != -1):
594 pos += 5
595 pos2 = data[0].find(" ", pos)
596 if (pos2 != -1):
597 parts = data[0][pos:pos2].split(":")
598 t = 0.0
599 for e in parts:
600 try:
601 v = float(e)
602 except:
603 continue
604 t = t * 60.0 + v
605 t /= self.final_length
606 self.progress_bar[1].set_fraction(t)
607 self.progress_bar[1].set_text("%.1f%%" % (100.0 * t))