测试游记

python 读取音频文件的详细信息

有一项测试的需求是判断录像是否和预期一致,检查项是:分辨率、录像时长等内容
刚开始考虑使用os模块直接获取文件的详细信息

文件的详细信息.png

os模块有一个stat()方法
  • st_mode: inode 保护模式

  • st_ino: inode 节点号。

  • st_dev: inode 驻留的设备。

  • st_nlink: inode 的链接数。

  • st_uid: 所有者的用户ID。

  • st_gid: 所有者的组ID。

  • st_size: 普通文件以字节为单位的大小;包含等待某些特殊文件的数据。

  • st_atime: 上次访问的时间。

  • st_mtime: 最后一次修改的时间。

  • st_ctime: 由操作系统报告的"ctime"。在某些系统上(如Unix)是最新的元数据更改的时间,在其它系统上(如Windows)是创建时间。
    查看了一下发现它只能获取文件的常规属性

    左侧是带外部标签的文件袋,右侧打开媒体文件袋后看到帧大小和音频波形。
    文件大小、修改时间是文件系统记录;画面分辨率、播放时长来自媒体本身。校验录像时,不能用文件字节数推断播放时长。
    文件的常规属性.png


    通过百度后找到了pymediainfo

pymediainfo 安装

1、使用pip安装

1pip install pymediainfo 

2、下载MediaInfo
点击MediaInfo下载MediaInfo,安装到本地,打开安装所在的路径

获取dll文件.png

将dll文件复制到 Python 根目录下
以上就完成了环境的搭建

开始获取音频文件的详细信息

1.从网上下载一个ts媒体文件

1from pymediainfo import MediaInfo
2media_info = MediaInfo.parse('C:\\Users\\Administrator\\Desktop\\test.ts')
3data = media_info.to_json()
4print(data)

更多用法欢迎去pymediainfo的官方文档查找,虽然我没找到
data中就包含了我们所需要的内容,大概长这样,不过里面不知道为什么有很多重复的内容,但是已经满足需求了。

一个媒体袋分别分出视频胶片和音频波形,两路显示不同属性。
一个媒体文件里的视频轨和音频轨可以有不同的属性。检查画面尺寸时找视频轨,检查声音时再看音频轨,不必把所有字段当成一份重复记录。
  1{
 2  "tracks": [
 3    {
 4      "track_type": "General",
 5      "count": "332",
 6      "count_of_stream_of_this_kind": "1",
 7      "kind_of_stream": "General",
 8      "other_kind_of_stream": [
 9        "General"
10      ],
11      "stream_identifier": "0",
12      "track_id": 1,
13      "other_track_id": [
14        "1 (0x1)"
15      ],
16      "count_of_video_streams": "1",
17      "count_of_audio_streams": "1",
18      "count_of_menu_streams": "1",
19      "video_format_list": "AVC",
20      "video_format_withhint_list": "AVC",
21      "codecs_video": "AVC",
22      "audio_format_list": "AAC",
23      "audio_format_withhint_list": "AAC",
24      "audio_codecs": "AAC LC",
25      "menu_format_list": "AVC / AAC",
26      "menu_format_withhint_list": "AVC / AAC",
27      "menu_codecs": "AVC / AAC LC",
28      "complete_name": "C:\\Users\\Administrator\\Desktop\\test.ts",
29      "folder_name": "C:\\Users\\Administrator\\Desktop",
30      "file_name": "test.ts",
31      "other_file_name": [
32        "test"
33      ],
34      "file_extension": "ts",
35      "format": "MPEG-TS",
36      "other_format": [
37        "MPEG-TS"
38      ],
39      "format_extensions_usually_used": "ts m2t m2s m4t m4s tmf ts tp trp ty",
40      "commercial_name": "MPEG-TS",
41      "internet_media_type": "video/MP2T",
42      "codec": "MPEG-TS",
43      "other_codec": [
44        "MPEG-TS"
45      ],
46      "codec_extensions_usually_used": "ts m2t m2s m4t m4s tmf ts tp trp ty",
47      "file_size": 4721808,
48      "other_file_size": [
49        "4.50 MiB",
50        "5 MiB",
51        "4.5 MiB",
52        "4.50 MiB",
53        "4.503 MiB"
54      ],
55      "duration": "31240.000000",
56      "other_duration": [
57        "31 s 240 ms",
58        "31 s 240 ms",
59        "31 s 240 ms",
60        "00:00:31.240",
61        "00:00:31.240"
62      ],
63      "overall_bit_rate_mode": "VBR",
64      "other_overall_bit_rate_mode": [
65        "Variable"
66      ],
67      "overall_bit_rate": 1208833,
68      "other_overall_bit_rate": [
69        "1 209 kb/s"
70      ],
71      "file_creation_date": "UTC 2018-08-26 04:05:47.439",
72      "file_creation_date__local": "2018-08-26 12:05:47.439",
73      "file_last_modification_date": "UTC 2018-08-26 04:05:49.220",
74      "file_last_modification_date__local": "2018-08-26 12:05:49.220",
75      "overallbitrate_precision_min": "1208813",
76      "overallbitrate_precision_max": "1208852"
77    },
78    {
79      "track_type": "Video",
80      "count": "344",
81      "count_of_stream_of_this_kind": "1",
82      "kind_of_stream": "Video",
83      "other_kind_of_stream": [
84        "Video"
85      ],
86      "stream_identifier": "0",
87      "streamorder": "0-0",
88      "track_id": 256,
89      "other_track_id": [
90        "256 (0x100)"
91      ],
92      "menu_id": 1,
93      "other_menu_id": [
94        "1 (0x1)"
95      ],
96      "format": "AVC",
97      "format_info": "Advanced Video Codec",
98      "format_url": "http://developers.videolan.org/x264.html",
99      "commercial_name": "AVC",
100      "format_profile": "Main@L3.1",
101      "format_settings": "CABAC / 4 Ref Frames",
102      "format_settings__cabac": "Yes",
103      "other_format_settings__cabac": [
104        "Yes"
105      ],
106      "format_settings__reframes": 4,
107      "other_format_settings__reframes": [
108        "4 frames"
109      ],
110      "internet_media_type": "video/H264",
111      "codec_id": "27",
112      "codec": "AVC",
113      "other_codec": [
114        "AVC"
115      ],
116      "codec_family": "AVC",
117      "codec_info": "Advanced Video Codec",
118      "codec_url": "http://developers.videolan.org/x264.html",
119      "codec_profile": "Main@L3.1",
120      "codec_settings": "CABAC / 4 Ref Frames",
121      "codec_settings__cabac": "Yes",
122      "codec_settings_refframes": "4",
123      "duration": 31280,
124      "other_duration": [
125        "31 s 280 ms",
126        "31 s 280 ms",
127        "31 s 280 ms",
128        "00:00:31.280",
129        "00:00:31.280"
130      ],
131      "nominal_bit_rate": 1024000,
132      "other_nominal_bit_rate": [
133        "1 024 kb/s"
134      ],
135      "width": 1280,
136      "other_width": [
137        "1 280 pixels"
138      ],
139      "height": 720,
140      "other_height": [
141        "720 pixels"
142      ],
143      "sampled_width": "1280",
144      "sampled_height": "720",
145      "pixel_aspect_ratio": "1.000",
146      "display_aspect_ratio": "1.778",
147      "other_display_aspect_ratio": [
148        "16:9"
149      ],
150      "frame_rate_mode": "VFR",
151      "other_frame_rate_mode": [
152        "Variable"
153      ],
154      "resolution": 8,
155      "other_resolution": [
156        "8 bits"
157      ],
158      "colorimetry": "4:2:0",
159      "color_space": "YUV",
160      "chroma_subsampling": "4:2:0",
161      "other_chroma_subsampling": [
162        "4:2:0"
163      ],
164      "bit_depth": 8,
165      "other_bit_depth": [
166        "8 bits"
167      ],
168      "scan_type": "Progressive",
169      "other_scan_type": [
170        "Progressive"
171      ],
172      "interlacement": "PPF",
173      "other_interlacement": [
174        "Progressive"
175      ],
176      "delay": "1481.000",
177      "other_delay": [
178        "1 s 481 ms",
179        "1 s 481 ms",
180        "1 s 481 ms",
181        "00:00:01.481"
182      ],
183      "delay__origin": "Container",
184      "other_delay__origin": [
185        "Container"
186      ],
187      "writing_library": "x264 - core 148",
188      "other_writing_library": [
189        "x264 core 148"
190      ],
191      "encoded_library_name": "x264",
192      "encoded_library_version": "core 148",
193      "encoding_settings": "cabac=1 / ref=1 / deblock=1:0:0 / analyse=0x1:0 / me=dia / subme=2 / psy=0 / mixed_ref=0 / me_range=16 / chroma_me=1 / trellis=0 / 8x8dct=0 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=0 / threads=4 / lookahead_threads=2 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=0 / open_gop=0 / weightp=2 / keyint=75 / keyint_min=25 / scenecut=60 / intra_refresh=0 / rc_lookahead=40 / rc=abr / mbtree=1 / bitrate=1024 / ratetol=1.0 / qcomp=0.40 / qpmin=0 / qpmax=69 / qpstep=4 / vbv_maxrate=1500 / vbv_bufsize=1500 / nal_hrd=none / filler=0 / ip_ratio=1.40 / aq=1:1.00"
194    },
195    {
196      "track_type": "Audio",
197      "count": "275",
198      "count_of_stream_of_this_kind": "1",
199      "kind_of_stream": "Audio",
200      "other_kind_of_stream": [
201        "Audio"
202      ],
203      "stream_identifier": "0",
204      "streamorder": "0-1",
205      "track_id": 257,
206      "other_track_id": [
207        "257 (0x101)"
208      ],
209      "menu_id": 1,
210      "other_menu_id": [
211        "1 (0x1)"
212      ],
213      "format": "AAC",
214      "format_info": "Advanced Audio Codec",
215      "commercial_name": "AAC",
216      "format_version": "Version 4",
217      "format_profile": "LC",
218      "muxing_mode": "ADTS",
219      "codec_id": "15",
220      "codec": "AAC LC",
221      "other_codec": [
222        "AAC LC"
223      ],
224      "duration": 31093,
225      "other_duration": [
226        "31 s 93 ms",
227        "31 s 93 ms",
228        "31 s 93 ms",
229        "00:00:31.093",
230        "00:00:31.093"
231      ],
232      "bit_rate_mode": "VBR",
233      "other_bit_rate_mode": [
234        "Variable"
235      ],
236      "channel_s": 2,
237      "other_channel_s": [
238        "2 channels"
239      ],
240      "channel_positions": "Front: L R",
241      "other_channel_positions": [
242        "2/0/0"
243      ],
244      "channellayout": "L R",
245      "samples_per_frame": "1024",
246      "sampling_rate": 44100,
247      "other_sampling_rate": [
248        "44.1 kHz"
249      ],
250      "samples_count": "1371201",
251      "frame_rate": "43.066",
252      "other_frame_rate": [
253        "43.066 FPS (1024 SPF)"
254      ],
255      "compression_mode": "Lossy",
256      "other_compression_mode": [
257        "Lossy"
258      ],
259      "delay": "1400.000",
260      "other_delay": [
261        "1 s 400 ms",
262        "1 s 400 ms",
263        "1 s 400 ms",
264        "00:00:01.400"
265      ],
266      "delay__origin": "Container",
267      "other_delay__origin": [
268        "Container"
269      ],
270      "delay_relative_to_video": -81,
271      "other_delay_relative_to_video": [
272        "-81 ms",
273        "-81 ms",
274        "-81 ms",
275        "-00:00:00.081"
276      ],
277      "video0_delay": -81,
278      "other_video0_delay": [
279        "-81 ms",
280        "-81 ms",
281        "-81 ms",
282        "-00:00:00.081"
283      ]
284    },
285    {
286      "track_type": "Menu",
287      "count": "94",
288      "count_of_stream_of_this_kind": "1",
289      "kind_of_stream": "Menu",
290      "other_kind_of_stream": [
291        "Menu"
292      ],
293      "stream_identifier": "0",
294      "streamorder": "0",
295      "track_id": 4096,
296      "other_track_id": [
297        "4096 (0x1000)"
298      ],
299      "menu_id": 1,
300      "other_menu_id": [
301        "1 (0x1)"
302      ],
303      "format": "AVC / AAC",
304      "commercial_name": "AVC / AAC",
305      "codec": "AVC / AAC LC",
306      "other_codec": [
307        "AVC / AAC LC"
308      ],
309      "duration": "31240.000000",
310      "other_duration": [
311        "31 s 240 ms",
312        "31 s 240 ms",
313        "31 s 240 ms",
314        "00:00:31.240",
315        "00:00:31.240"
316      ],
317      "delay": "1401.000000",
318      "other_delay": [
319        "1 s 401 ms",
320        "1 s 401 ms",
321        "1 s 401 ms",
322        "00:00:01.401"
323      ],
324      "list_streamkind": "1 / 2",
325      "list_streampos": "0 / 0",
326      "list": "256 / 257",
327      "other_list": [
328        "256 (0x100) (AVC) / 257 (0x101) (AAC)"
329      ],
330      "service_name": "Service01",
331      "service_provider": "FFmpeg",
332      "service_type": "digital television",
333      "pointer_field": "0",
334      "section_length": "23"
335    }
336  ]
337}

本文视频讲解

打开或下载 MP4 ↗

评论与讨论

使用 GitHub 账号留言,评论公开显示。

搜索文章