This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
return_video_ratios.py
99 lines (90 loc) · 3.63 KB
/
return_video_ratios.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
##############################################################################################
# This file is deprecated because Python 2.x is deprecated #
# A Python 3.x version of this file can be found at: #
# #
# https://github.com/Guymer/PyGuymer3/blob/master/return_video_ratios.py #
##############################################################################################
def return_video_ratios(fname, playlist = None):
# Check input ...
if fname.startswith(u"bluray:") and playlist is None:
raise Exception("a Blu-ray was specified but no playlist was supplied")
# Import modules ...
import json
import subprocess
# Load sub-functions ...
from .find_integer_divisors import find_integer_divisors
# Find stream info ...
if fname.startswith(u"bluray:"):
proc = subprocess.Popen(
[
u"ffprobe",
u"-loglevel", u"quiet",
u"-probesize", u"3G",
u"-analyzeduration", u"1800M",
u"-print_format", u"json",
u"-show_streams",
u"-playlist", u"{0:d}".format(playlist),
fname
],
stderr = subprocess.PIPE,
stdout = subprocess.PIPE
)
stdout, stderr = proc.communicate()
if proc.returncode != 0:
raise Exception(u"\"ffprobe\" command failed")
else:
proc = subprocess.Popen(
[
u"ffprobe",
u"-loglevel", u"quiet",
u"-probesize", u"3G",
u"-analyzeduration", u"1800M",
u"-print_format", u"json",
u"-show_streams",
fname
],
stderr = subprocess.PIPE,
stdout = subprocess.PIPE
)
stdout, stderr = proc.communicate()
if proc.returncode != 0:
# HACK: Fallback and attempt to load it as a raw M-JPEG stream.
proc = subprocess.Popen(
[
u"ffprobe",
u"-loglevel", u"quiet",
u"-probesize", u"3G",
u"-analyzeduration", u"1800M",
u"-print_format", u"json",
u"-show_streams",
u"-f", u"mjpeg",
fname
],
stderr = subprocess.PIPE,
stdout = subprocess.PIPE
)
stdout, stderr = proc.communicate()
if proc.returncode != 0:
raise Exception(u"\"ffprobe\" command failed")
# Loop over streams ...
for stream in json.loads(stdout)[u"streams"]:
# Skip stream if it is not video ...
if stream[u"codec_type"].strip().lower() != u"video":
continue
# Find common dimensions divisors ...
w_divs = find_integer_divisors(stream[u"width"])
h_divs = find_integer_divisors(stream[u"height"])
fact = 1
for w_div in reversed(w_divs):
if w_div in h_divs:
fact = w_div
break
# Create short-hands and then return them ...
# NOTE: "ffmpeg" incorrectly calls PAR "sample aspect ratio".
dar = stream[u"display_aspect_ratio"]
par = stream[u"sample_aspect_ratio"]
sar = u"{0:d}:{1:d}".format(stream[u"width"] / fact, stream[u"height"] / fact)
return dar, par, sar
# Return error ...
return u"ERROR", u"ERROR", u"ERROR"