-
Notifications
You must be signed in to change notification settings - Fork 2
/
ffmsinfo.py
executable file
·180 lines (159 loc) · 5.85 KB
/
ffmsinfo.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
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
"""Extract information from media files.
"""
import argparse
import os
import sys
import time
import ffms2.console_mode # @UnusedImport
AUDIO_FORMATS = ["8-bit", "16-bit", "32-bit", "float", "double"]
TYPES = ["video", "audio", "data", "subtitles", "attachment"]
def init_progress_callback(
msg="Indexing...", time_threshold=1, check_time=0.2
):
def ic(current, total, private=None):
pct = current * 100 // total
if ic.show_pct:
if pct > ic.pct:
ic.pct = pct
print("\r{} {:d}%".format(msg, pct))
elif time.time() - start_time >= check_time and pct < pct_threshold:
ic.show_pct = True
return 0
def done():
ic(1, 1)
print()
print(msg)
ic.done = done
ic.pct = -1
ic.show_pct = True
pct_threshold = int(check_time * 100 / time_threshold)
start_time = time.time()
return ic
def parse_args():
parser = argparse.ArgumentParser("ffmsinfo")
parser.add_argument("source_files", type=str, nargs="+")
parser.add_argument(
"-w",
"--disable-write-index",
dest="write_index",
action="store_false",
help="disable writing index to disk",
)
parser.add_argument(
"-p",
"--disable-progress",
dest="progress",
action="store_false",
help="disable indexing progress reporting",
)
parser.add_argument(
"--version",
action="version",
version="FFMS {}".format(ffms2.get_version()),
help="show FFMS version number",
)
return parser.parse_args()
def create_index(indexer, write_index=True, progress=True, msg="Indexing…"):
ic = init_progress_callback(msg) if progress else None
indexer.set_progress_callback(ic)
index = indexer.do_indexing2()
if ic:
ic.done()
if write_index:
try:
index.write()
except ffms2.Error as e:
print(e, file=sys.stderr)
return index
def main():
args = parse_args()
source_files = args.source_files
for source_file in source_files:
print(source_file)
try:
indexer = ffms2.Indexer(source_file)
for track_info in indexer.track_info_list:
indexer.track_index_settings(track_info.num, 1, 0)
except ffms2.Error as e:
print(e, file=sys.stderr)
else:
format_name = indexer.format_name
track_info_list = indexer.track_info_list
index_file = source_file + ffms2.FFINDEX_EXT
if os.path.isfile(index_file):
recreate_index = False
try:
index = ffms2.Index.read(index_file, source_file)
except ffms2.Error as e:
recreate_index = True
print(e, file=sys.stderr)
else:
for track in index.tracks:
if (
track.type == ffms2.FFMS_TYPE_AUDIO
and not track.frame_info_list
):
recreate_index = True
break
if recreate_index:
index = create_index(
indexer,
args.write_index,
args.progress,
"Reindexing...",
)
else:
index = create_index(indexer, args.write_index, args.progress)
print("format =", format_name)
for (n, type_, codec_name) in track_info_list:
type_name = (
TYPES[type_] if 0 <= type_ < len(TYPES) else "unknown"
)
print("{}:".format(n))
print("\ttype =", type_name)
print("\tcodec =", codec_name)
if type_ == ffms2.FFMS_TYPE_VIDEO:
vsource = ffms2.VideoSource(source_file, n, index)
vprops = vsource.properties
frame = vsource.get_frame(0)
sar_num, sar_den = (
(vprops.SARNum, vprops.SARDen)
if vprops.SARNum and vprops.SARDen
else (1, 1)
)
aspect_ratio = (
frame.EncodedWidth
* sar_num
/ sar_den
/ frame.EncodedHeight
)
print(
"\tresolution =",
"{}×{}".format(
frame.EncodedWidth, frame.EncodedHeight
),
)
print("\taspect ratio =", aspect_ratio)
print(
"\tfps =", vprops.FPSNumerator / vprops.FPSDenominator
)
print("\tduration =", vprops.LastTime)
print("\tnum frames =", vprops.NumFrames)
elif type_ == ffms2.FFMS_TYPE_AUDIO:
asource = ffms2.AudioSource(source_file, n, index)
aprops = asource.properties
sample_format_name = (
AUDIO_FORMATS[aprops.SampleFormat]
if 0 <= aprops.SampleFormat < len(AUDIO_FORMATS)
else "unknown"
)
print("\tsample rate =", aprops.SampleRate),
print("\tbits per sample =", aprops.BitsPerSample)
print("\tsample format =", sample_format_name)
print("\tnum channels =", aprops.Channels)
print("\tduration =", aprops.LastTime)
print("\tnum samples =", aprops.NumSamples)
print()
if __name__ == "__main__":
sys.exit(main())