-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreams.py
111 lines (93 loc) · 3.87 KB
/
Streams.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
100
101
102
103
104
105
106
107
108
109
110
111
__author__ = 'Jonathan Bondhus'
import os
import subprocess
import json
from tabulate import tabulate
from Stream import Stream
class Streams:
def __init__(self, path):
# Get stream data from the file
data = json.loads(subprocess.check_output(
"ffprobe -v quiet -print_format json -show_format -show_streams \"" + os.path.abspath(path) + "\"",
shell=True, stderr=subprocess.STDOUT).decode("utf-8"))
self.streams = []
self.audio_stream_mapping = {}
self.subtitle_stream_mapping = {}
for stream in data['streams']:
try:
title = stream['tags']['title']
except KeyError:
title = "none"
try:
language = stream['tags']['language']
except KeyError:
language = "unknown"
try:
bitrate = stream['bit_rate']
except KeyError:
bitrate = "unknown"
try:
codec_short_name = stream['codec_name']
except KeyError:
codec_short_name = "unknown"
try:
codec_long_name = stream['codec_long_name']
except KeyError:
codec_long_name = "unknown"
try:
channels = stream['channels']
except KeyError:
channels = "unknown"
try:
channel_layout = stream['channel_layout']
except KeyError:
channel_layout = "unknown"
try:
profile = stream['profile']
except KeyError:
profile = "unknown"
# Add the current stream to the list of streams
self.streams.append(Stream(stream['index'], title, stream['codec_type'], language, bitrate,
codec_short_name, codec_long_name, channels, channel_layout, profile))
def get_video_streams(self):
video_streams = []
for stream in self.streams:
if stream.stream_type == "video":
video_streams.append(stream)
return video_streams
def get_audio_streams(self):
audio_streams = []
for stream in self.streams:
if stream.stream_type == "audio":
audio_streams.append(stream)
return audio_streams
def get_subtitle_streams(self):
subtitle_streams = []
for stream in self.streams:
if stream.stream_type == "subtitle":
subtitle_streams.append(stream)
return subtitle_streams
def list_audio_streams(self):
audio_streams = []
i = 1
for stream in self.streams:
if stream.stream_type == "audio":
audio_streams.append([str(i), stream.stream_type, stream.language, stream.bitrate,
stream.codec_short_name, stream.codec_long_name,
stream.channels, stream.channel_layout, stream.profile])
self.audio_stream_mapping[str(i)] = str(stream.index)
i += 1
return tabulate(audio_streams,
headers=["ID", "Title", "Language", "Bitrate", "Codec", "Codec Description", "# of Channels",
"Channel Layout", "Profile"], tablefmt="grid")
def list_subtitle_streams(self):
subtitle_streams = []
i = 1
for stream in self.streams:
if stream.stream_type == "subtitle":
subtitle_streams.append(
[str(i), stream.title, stream.language, stream.codec_short_name, stream.codec_long_name])
self.subtitle_stream_mapping[str(i)] = str(stream.index)
i += 1
return tabulate(subtitle_streams, headers=["ID", "Title", "Bitrate", "Codec", "Codec Description"],
tablefmt="grid")