Debian Codegraph @

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