| 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 avprobe(devedeng.avbase.avbase): |
| 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 = "AVPROBE" |
| 35 |
|
| 36 |
@staticmethod |
| 37 |
def check_is_installed(): |
| 38 |
try: |
| 39 |
handle = subprocess.Popen( |
| 40 |
["avprobe", "-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 = ["avprobe", file_name, "-of", |
| 67 |
"json", "-show_streams", "-loglevel", "quiet"] |
| 68 |
|
| 69 |
(stdout, stderr) = self.launch_process(command_line, False) |
| 70 |
|
| 71 |
try: |
| 72 |
stdout2 = stdout.decode("utf-8") |
| 73 |
except: |
| 74 |
stdout2 = stdout.decode("latin1") |
| 75 |
self.config.append_static_log("AVProbe JSON data: " + str(stdout2)) |
| 76 |
return self.process_json(stdout2, file_name) |
| 77 |
|
| 78 |
def process_json(self, stdout2, file_name): |
| 79 |
|
| 80 |
self.audio_list = [] |
| 81 |
self.audio_streams = 0 |
| 82 |
self.video_list = [] |
| 83 |
self.video_streams = 0 |
| 84 |
self.original_width = 0 |
| 85 |
self.original_height = 0 |
| 86 |
self.original_length = -1 |
| 87 |
self.original_videorate = 0 |
| 88 |
self.original_audiorate = 0 |
| 89 |
self.original_audiorate_uncompressed = 0 |
| 90 |
self.original_fps = 0 |
| 91 |
self.original_aspect_ratio = None |
| 92 |
self.prerotation = 0 |
| 93 |
|
| 94 |
self.config.append_static_log( |
| 95 |
"Getting data for {:s} with avprobe".format(file_name)) |
| 96 |
try: |
| 97 |
video_data = json.loads(stdout2) |
| 98 |
except: |
| 99 |
return True # There was an error reading the JSON data |
| 100 |
|
| 101 |
if not("streams" in video_data): |
| 102 |
return True # There are no streams!!!!! |
| 103 |
|
| 104 |
for element in video_data["streams"]: |
| 105 |
|
| 106 |
if (self.original_length == -1) and ("duration" in element): |
| 107 |
try: |
| 108 |
self.original_length = int(float(element["duration"])) |
| 109 |
except: |
| 110 |
self.original_length = -1 |
| 111 |
|
| 112 |
if (element["codec_type"] == "video"): |
| 113 |
if "tags" in element: |
| 114 |
tags = element["tags"] |
| 115 |
if "rotate" in tags: |
| 116 |
self.prerotation = int(tags["rotate"]) |
| 117 |
self.video_streams += 1 |
| 118 |
self.video_list.append(element["index"]) |
| 119 |
if (self.video_streams == 1): |
| 120 |
self.original_width = int(float(element["width"])) |
| 121 |
self.original_height = int(float(element["height"])) |
| 122 |
if ("bit_rate" in element): |
| 123 |
self.original_videorate = int( |
| 124 |
float(element["bit_rate"])) / 1000 |
| 125 |
self.original_fps = self.get_division( |
| 126 |
element["avg_frame_rate"]) |
| 127 |
if ("display_aspect_ratio" in element): |
| 128 |
self.original_aspect_ratio = self.get_division(element["display_aspect_ratio"]) |
| 129 |
|
| 130 |
elif (element["codec_type"] == "audio"): |
| 131 |
self.audio_streams += 1 |
| 132 |
self.audio_list.append(element["index"]) |
| 133 |
if (self.audio_streams == 1): |
| 134 |
if ("bit_rate" in element): |
| 135 |
self.original_audiorate = int(float(element["bit_rate"])) / 1000 |
| 136 |
self.original_audiorate_uncompressed = int( |
| 137 |
float(element["sample_rate"])) |
| 138 |
|
| 139 |
self.original_size = str(self.original_width) + "x" + str(self.original_height) |
| 140 |
if (self.original_aspect_ratio is None): |
| 141 |
if (self.original_height != 0): |
| 142 |
self.original_aspect_ratio = (float(self.original_width)) / (float(self.original_height)) |
| 143 |
else: |
| 144 |
self.original_aspect_ratio = 1 |
| 145 |
|
| 146 |
self.original_aspect_ratio = (float(int(self.original_aspect_ratio * 1000.0))) / 1000.0 |
| 147 |
|
| 148 |
if (len(self.video_list) == 0): |
| 149 |
return True # the file is not a video file; maybe an audio-only file or another thing |
| 150 |
|
| 151 |
if self.original_length == -1: # if it was unable to detect the duration, try to use the human readable format |
| 152 |
command_line = ["avprobe", file_name] |
| 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( |
| 161 |
"Using avprobe human readable format: " + str(stdout2)) |
| 162 |
for line in stdout2.split("\n"): |
| 163 |
line = line.strip() |
| 164 |
if line.startswith("Duration: "): |
| 165 |
self.original_length = self.get_time(line[10:]) |
| 166 |
break |
| 167 |
|
| 168 |
self.config.append_static_log("Estimated length: {:d}; Resolution: {:s}".format( |
| 169 |
self.original_length, self.original_size)) |
| 170 |
return False # no error |