-
Notifications
You must be signed in to change notification settings - Fork 4
/
video_info.py
170 lines (134 loc) · 4.91 KB
/
video_info.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
Video Information Class
=======================
Uses ffmpeg to grab video and audio info from a specified file and stores
these values internally
Usage:
------
>>> this = VideoObject('video.avi')
>>> this.printinfo()
VIDEO INFO
==========
Filename: video.avi
Video Length: 244.84 seconds
Video Codec: msmpeg4v2
Video Size: 368x208
Frame Rate: 25.0 fps
Video Bitrate: 782 kb/s
AUDIO INFO:
===========
Audio Codec: mp3
Sampling Freq: 44100 Hz
Audio Bitrate: 96 kb/s
"""
from subprocess import Popen, PIPE, STDOUT
import re
class VideoObject:
def __init__(self, infile):
self.source = infile
self.output = None
self.length = None #
self.video_codec = None #
self.video_size = None #
self.frame_rate = None #
self.video_bitrate = None #
self.audio_codec = None #
self.audio_freq = None #
self.audio_bitrate = None
if self.source:
# Get the output of the ffmpeg call
self.grab_source()
# Run through the various regex functions to get the info needed
self.get_framerate()
self.get_length()
self.get_size()
self.get_video_codec()
self.get_video_bitrate()
self.get_audio_codec()
self.get_audio_freq()
self.get_audio_bitrate()
def grab_source(self):
commandline = 'ffmpeg -i ' + self.source
outcode = Popen(commandline, stdout=PIPE, stderr=PIPE)
self.output = outcode.stderr.read()
def get_framerate(self):
output = self.output
ex = r' \d+.\d+ tb\(r\)'
n = re.compile(ex)
found = n.findall(output)
rate = 0.0
if found:
rate = float(found[0].replace('tb(r)', '').replace(' ', ''))
self.frame_rate = rate
def get_length(self):
output = self.output
ex = r'Duration: \d+:\d+:\d+.\d+'
n = re.compile(ex)
found = n.search(output)
hours, min, sec = 0.0, 0.0, 0.0
if found:
clean = found.group(0).replace('Duration: ', '').replace(' ', '')
ar = clean.split(':')
hours = float(ar[0])
min = float(ar[1])
sec = float(ar[2])
self.length = self.get_seconds(hours, min, sec)
def get_seconds(self, hr, min, sec):
secs = ((hr * 60) * 60) + (min * 60) + sec
return secs
def get_size(self):
output = self.output
ex = r'\d+x\d+'
n = re.compile(ex)
found = n.findall(output)
if found:
self.video_size = found[0]
def get_video_codec(self):
output = self.output
ex = r'Video: \w+'
n = re.compile(ex)
found = n.findall(output)
if found:
self.video_codec = found[0].replace('Video: ', '').replace(' ', '')
def get_video_bitrate(self):
output = self.output
ex = r'\d+ kb/s'
n = re.compile(ex)
found = n.findall(output)
if found:
self.video_bitrate = found[0].replace('kb/s', '').replace(' ', '')
def get_audio_codec(self):
output = self.output
ex = r'Audio: \w+'
n = re.compile(ex)
found = n.findall(output)
if found:
self.audio_codec = found[0].replace('Audio: ', '').replace(' ', '')
def get_audio_freq(self):
output = self.output
ex = r'\d+ Hz'
n = re.compile(ex)
found = n.findall(output)
if found:
self.audio_freq = found[0].replace(' Hz', '').replace(' ', '')
def get_audio_bitrate(self):
output = self.output
ex = r'\d+ kb/s'
n = re.compile(ex)
found = n.findall(output)
if found:
self.audio_bitrate = found[1].replace('kb/s', '').replace(' ', '')
def printinfo(self):
print "\nVIDEO INFO"
print "=========="
print "Filename: \t" + self.source
print "Video Length: \t" + str(self.length) + " seconds"
print "Video Codec: \t" + self.video_codec
print "Video Size: \t" + self.video_size
print "Frame Rate: \t" + str(self.frame_rate) + " fps"
print "Video Bitrate: \t" + self.video_bitrate + " kb/s"
print "\nAUDIO INFO:"
print "==========="
print "Audio Codec: \t" + self.audio_codec
print "Sampling Freq: \t" + self.audio_freq + " Hz"
print "Audio Bitrate: \t" + self.audio_bitrate + " kb/s"