forked from PeterL1n/RobustVideoMatting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_utils.py
75 lines (59 loc) · 2.17 KB
/
video_utils.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
import ffmpeg
import json
import sys
import glob
def get_file_name(input):
file_list = glob.glob(input)
file_list.sort()
file_list = [n for n in file_list if "_depth" not in n]
return file_list[-1]
class VideoInfo:
Path = ""
Width = 0
Height = 0
FPS = 0
NumFrames = 0
DurationInSeconds = 0
Bitrate = 0
Codec = ""
AudioBitrate = 0
AudioChannels = 0
AudioSampleRate = 0
AudioSampleRate = 0
AudioCodec = ""
def __init__(self, path):
self.Path = path
self.load()
def __str__(self) -> str:
return f'Video path: {self.Path}, width:{self.Width}, height:{self.Height}, fps:{self.FPS}, '\
f'num_frames:{self.NumFrames}, duration_in_seconds:{self.DurationInSeconds}, '\
f'bitrate:{self.Bitrate}, codec:{self.Codec}, '\
f'audio_bitrate:{self.AudioBitrate}, audio_channels:{self.AudioChannels}, '\
f'audio_sample_rate:{self.AudioSampleRate}, audio_codec:{self.AudioCodec}'
def load(self):
probe = ffmpeg.probe(self.Path)
# print(json.dumps(probe))
video_info = {}
audio_info = {}
for s in probe['streams']:
if s['codec_type'] == 'video':
video_info = s
if s['codec_type'] == 'audio':
audio_info = s
print(f"video_info: {video_info}, audio_info: {audio_info}")
self.Width = int(video_info['width'])
self.Height = int(video_info['height'])
self.FPS = int(video_info['r_frame_rate'].split('/')[0])
self.NumFrames = int(video_info['nb_frames'])
self.DurationInSeconds = float(video_info['duration'])
self.Bitrate = int(video_info['bit_rate'])
self.Codec = video_info['codec_name']
self.AudioBitrate = int(audio_info.get('bit_rate', '0'))
self.AudioChannels = int(audio_info.get('channels', '0'))
self.AudioSampleRate = int(audio_info.get('sample_rate', '0'))
self.AudioCodec = audio_info.get('codec_name', "")
if __name__ == "__main__":
filename = sys.argv[1]
print(f"filename: {filename}")
video_info = VideoInfo(filename)
print(f"video_info: {video_info}")