|
1
|
|
|
""" |
|
2
|
|
|
ffmpeg_streaming._ffprobe |
|
3
|
|
|
~~~~~~~~~~~~ |
|
4
|
|
|
|
|
5
|
|
|
Probe the video |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
:copyright: (c) 2020 by Amin Yazdanpanah. |
|
9
|
|
|
:website: https://www.aminyazdanpanah.com |
|
10
|
|
|
:email: [email protected] |
|
11
|
|
|
:license: MIT, see LICENSE for more details. |
|
12
|
|
|
""" |
|
13
|
|
|
|
|
14
|
|
|
import json |
|
15
|
|
|
import logging |
|
16
|
|
|
import subprocess |
|
17
|
|
|
|
|
18
|
|
|
from ._media_streams import Streams |
|
19
|
|
|
from ._media_property import Size, Bitrate |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
class FFProbe: |
|
23
|
|
|
def __init__(self, filename, cmd='ffprobe'): |
|
24
|
|
|
commands = [cmd, '-show_format', '-show_streams', '-of', 'json', filename] |
|
25
|
|
|
logging.info("ffprobe running command: {}".format(" ".join(commands))) |
|
26
|
|
|
process = subprocess.Popen(commands, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
27
|
|
|
self.out, err = process.communicate() |
|
28
|
|
|
if process.returncode != 0: |
|
29
|
|
|
logging.error(str(self.out) + str(err)) |
|
30
|
|
|
raise RuntimeError('ffprobe', self.out, err) |
|
31
|
|
|
logging.info("ffprobe executed command successfully!") |
|
32
|
|
|
|
|
33
|
|
|
def streams(self): |
|
34
|
|
|
return Streams(json.loads(self.out.decode('utf-8'))['streams']) |
|
35
|
|
|
|
|
36
|
|
|
def format(self): |
|
37
|
|
|
return json.loads(self.out.decode('utf-8'))['format'] |
|
38
|
|
|
|
|
39
|
|
|
def all(self): |
|
40
|
|
|
return json.loads(self.out.decode('utf-8')) |
|
41
|
|
|
|
|
42
|
|
|
def save_as_json(self, path): |
|
43
|
|
|
with open(path, 'w') as probe: |
|
44
|
|
|
probe.write(self.out.decode('utf-8')) |
|
45
|
|
|
|
|
46
|
|
|
@property |
|
47
|
|
|
def video_size(self) -> Size: |
|
48
|
|
|
width = int(self.streams().video().get('width', 0)) |
|
49
|
|
|
height = int(self.streams().video().get('height', 0)) |
|
50
|
|
|
|
|
51
|
|
|
if width == 0 or height == 0: |
|
52
|
|
|
raise RuntimeError('It could not determine the value of width/height') |
|
53
|
|
|
|
|
54
|
|
|
return Size(width, height) |
|
55
|
|
|
|
|
56
|
|
|
@property |
|
57
|
|
|
def bitrate(self, _type: str = "k") -> Bitrate: |
|
58
|
|
|
overall = int(self.format().get('bit_rate', 0)) |
|
59
|
|
|
video = int(self.streams().video().get('bit_rate', 0)) |
|
60
|
|
|
audio = int(self.streams().audio().get('bit_rate', 0)) |
|
61
|
|
|
|
|
62
|
|
|
if overall == 0: |
|
63
|
|
|
raise RuntimeError('It could not determine the value of bitrate') |
|
64
|
|
|
|
|
65
|
|
|
return Bitrate(video, audio, overall, type=_type) |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
def ffprobe(filename, cmd='ffprobe'): |
|
69
|
|
|
return FFProbe(filename, cmd) |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
__all__ = [ |
|
73
|
|
|
'FFProbe' |
|
74
|
|
|
] |
|
75
|
|
|
|