Debian Codegraph @

src/devedeng/ffprobe.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 devedeng.configuration_data
21 import devedeng.executor
22 import os
23 import json
24
25
26 class ffprobe(devedeng.executor.executor):
27
28 supports_analize = True
29 supports_play = False
30 supports_convert = False
31 supports_menu = False
32 supports_mkiso = False
33 supports_burn = False
34 display_name = "FFPROBE"
35
36 @staticmethod
37 def check_is_installed():
38 try:
39 handle = subprocess.Popen(
40 ["ffprobe", "-h"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
41 (stdout, stderr) = handle.communicate()
42 if 0 == handle.wait():
43 return True
44 else:
45 return False
46 except:
47 return False
48
49 def __init__(self):
50
51 devedeng.executor.executor.__init__(self)
52 self.config = devedeng.configuration_data.configuration.get_config()
53
54 def process_stdout(self, data):
55 return
56
57 def process_stderr(self, data):
58 return
59
60 def get_film_data(self, file_name):
61 """ processes a file, refered by the FILE_MOVIE movie object, and fills its
62 main data (resolution, FPS, length...) """
63
64 self.original_file_size = os.path.getsize(file_name)
65
66 command_line = ["ffprobe", file_name, "-of",
67 "json", "-show_streams", "-loglevel", "quiet"]
68
69 (stdout, stderr) = self.launch_process(command_line, False)
70 try:
71 stdout2 = stdout.decode("utf-8")
72 except:
73 stdout2 = stdout.decode("latin1")
74 self.config.append_static_log("FFProbe JSON data: " + str(stdout2))
75 return self.process_json(file_name, stdout2)
76
77 def process_json(self, file_name, stdout2):
78
79 self.audio_list = []
80 self.audio_streams = 0
81 self.video_list = []
82 self.video_streams = 0
83 self.original_width = 0
84 self.original_height = 0
85 self.original_length = -1
86 self.original_videorate = 0
87 self.original_audiorate = 0
88 self.original_audiorate_uncompressed = 0
89 self.original_fps = 0
90 self.original_aspect_ratio = None
91 self.prerotation = 0
92
93 self.config.append_static_log(
94 "Getting data for {:s} with ffprobe".format(file_name))
95 try:
96 video_data = json.loads(stdout2)
97 except:
98 return True # There was an error reading the JSON data
99
100 if not("streams" in video_data):
101 return True # There are no streams!!!!!
102
103 for element in video_data["streams"]:
104
105 if (self.original_length == -1) and ("duration" in element):
106 try:
107 self.original_length = int(float(element["duration"]))
108 except:
109 self.original_length = -1
110
111 if (element["codec_type"] == "video"):
112 if "tags" in element:
113 tags = element["tags"]
114 if "rotate" in tags:
115 self.prerotation = int(tags["rotate"])
116 self.video_streams += 1
117 self.video_list.append(element["index"])
118 if (self.video_streams == 1):
119 self.original_width = int(float(element["width"]))
120 self.original_height = int(float(element["height"]))
121 if ("bit_rate" in element):
122 self.original_videorate = int(
123 float(element["bit_rate"])) / 1000
124 self.original_fps = self.get_division(
125 element["avg_frame_rate"])
126 if ("display_aspect_ratio" in element):
127 self.original_aspect_ratio = self.get_division(element["display_aspect_ratio"])
128
129 elif (element["codec_type"] == "audio"):
130 self.audio_streams += 1
131 self.audio_list.append(element["index"])
132 if (self.audio_streams == 1):
133 if ("bit_rate" in element):
134 self.original_audiorate = int(float(element["bit_rate"])) / 1000
135 self.original_audiorate_uncompressed = int(
136 float(element["sample_rate"]))
137
138 self.original_size = str(self.original_width) + "x" + str(self.original_height)
139 if (self.original_aspect_ratio is None):
140 if (self.original_height != 0):
141 self.original_aspect_ratio = (float(self.original_width)) / (float(self.original_height))
142 else:
143 self.original_aspect_ratio = 1
144
145 self.original_aspect_ratio = (float(int(self.original_aspect_ratio * 1000.0))) / 1000.0
146
147 if (len(self.video_list) == 0):
148 return True # the file is not a video file; maybe an audio-only file or another thing
149
150 if self.original_length == -1: # if it was unable to detect the duration, try to use the human readable format
151 command_line = ["ffprobe", file_name]
152 print(f"Launching {command_line}")
153 (stdout, stderr) = self.launch_process(command_line, False)
154 try:
155 stdout2 = stdout.decode("utf-8") + \
156 "\n" + stderr.decode("utf-8")
157 except:
158 stdout2 = stdout.decode("latin1") + \
159 "\n" + stderr.decode("latin1")
160 self.config.append_static_log("Using ffprobe human readable format: " + str(stdout2))
161 for line in stdout2.split("\n"):
162 line = line.strip()
163 if line.startswith("Duration: "):
164 self.original_length = self.get_time(line[10:])
165 break
166
167 self.config.append_static_log("Estimated length: {:d}; Resolution: {:s}".format(self.original_length, self.original_size))
168 return False # no error