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安装

pip install pymediainfo 

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

获取dll文件.png


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

 

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

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

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

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

{
  "tracks": [
    {
      "track_type": "General",
      "count": "332",
      "count_of_stream_of_this_kind": "1",
      "kind_of_stream": "General",
      "other_kind_of_stream": [
        "General"
      ],
      "stream_identifier": "0",
      "track_id": 1,
      "other_track_id": [
        "1 (0x1)"
      ],
      "count_of_video_streams": "1",
      "count_of_audio_streams": "1",
      "count_of_menu_streams": "1",
      "video_format_list": "AVC",
      "video_format_withhint_list": "AVC",
      "codecs_video": "AVC",
      "audio_format_list": "AAC",
      "audio_format_withhint_list": "AAC",
      "audio_codecs": "AAC LC",
      "menu_format_list": "AVC / AAC",
      "menu_format_withhint_list": "AVC / AAC",
      "menu_codecs": "AVC / AAC LC",
      "complete_name": "C:\\Users\\Administrator\\Desktop\\test.ts",
      "folder_name": "C:\\Users\\Administrator\\Desktop",
      "file_name": "test.ts",
      "other_file_name": [
        "test"
      ],
      "file_extension": "ts",
      "format": "MPEG-TS",
      "other_format": [
        "MPEG-TS"
      ],
      "format_extensions_usually_used": "ts m2t m2s m4t m4s tmf ts tp trp ty",
      "commercial_name": "MPEG-TS",
      "internet_media_type": "video/MP2T",
      "codec": "MPEG-TS",
      "other_codec": [
        "MPEG-TS"
      ],
      "codec_extensions_usually_used": "ts m2t m2s m4t m4s tmf ts tp trp ty",
      "file_size": 4721808,
      "other_file_size": [
        "4.50 MiB",
        "5 MiB",
        "4.5 MiB",
        "4.50 MiB",
        "4.503 MiB"
      ],
      "duration": "31240.000000",
      "other_duration": [
        "31 s 240 ms",
        "31 s 240 ms",
        "31 s 240 ms",
        "00:00:31.240",
        "00:00:31.240"
      ],
      "overall_bit_rate_mode": "VBR",
      "other_overall_bit_rate_mode": [
        "Variable"
      ],
      "overall_bit_rate": 1208833,
      "other_overall_bit_rate": [
        "1 209 kb/s"
      ],
      "file_creation_date": "UTC 2018-08-26 04:05:47.439",
      "file_creation_date__local": "2018-08-26 12:05:47.439",
      "file_last_modification_date": "UTC 2018-08-26 04:05:49.220",
      "file_last_modification_date__local": "2018-08-26 12:05:49.220",
      "overallbitrate_precision_min": "1208813",
      "overallbitrate_precision_max": "1208852"
    },
    {
      "track_type": "Video",
      "count": "344",
      "count_of_stream_of_this_kind": "1",
      "kind_of_stream": "Video",
      "other_kind_of_stream": [
        "Video"
      ],
      "stream_identifier": "0",
      "streamorder": "0-0",
      "track_id": 256,
      "other_track_id": [
        "256 (0x100)"
      ],
      "menu_id": 1,
      "other_menu_id": [
        "1 (0x1)"
      ],
      "format": "AVC",
      "format_info": "Advanced Video Codec",
      "format_url": "http://developers.videolan.org/x264.html",
      "commercial_name": "AVC",
      "format_profile": "Main@L3.1",
      "format_settings": "CABAC / 4 Ref Frames",
      "format_settings__cabac": "Yes",
      "other_format_settings__cabac": [
        "Yes"
      ],
      "format_settings__reframes": 4,
      "other_format_settings__reframes": [
        "4 frames"
      ],
      "internet_media_type": "video/H264",
      "codec_id": "27",
      "codec": "AVC",
      "other_codec": [
        "AVC"
      ],
      "codec_family": "AVC",
      "codec_info": "Advanced Video Codec",
      "codec_url": "http://developers.videolan.org/x264.html",
      "codec_profile": "Main@L3.1",
      "codec_settings": "CABAC / 4 Ref Frames",
      "codec_settings__cabac": "Yes",
      "codec_settings_refframes": "4",
      "duration": 31280,
      "other_duration": [
        "31 s 280 ms",
        "31 s 280 ms",
        "31 s 280 ms",
        "00:00:31.280",
        "00:00:31.280"
      ],
      "nominal_bit_rate": 1024000,
      "other_nominal_bit_rate": [
        "1 024 kb/s"
      ],
      "width": 1280,
      "other_width": [
        "1 280 pixels"
      ],
      "height": 720,
      "other_height": [
        "720 pixels"
      ],
      "sampled_width": "1280",
      "sampled_height": "720",
      "pixel_aspect_ratio": "1.000",
      "display_aspect_ratio": "1.778",
      "other_display_aspect_ratio": [
        "16:9"
      ],
      "frame_rate_mode": "VFR",
      "other_frame_rate_mode": [
        "Variable"
      ],
      "resolution": 8,
      "other_resolution": [
        "8 bits"
      ],
      "colorimetry": "4:2:0",
      "color_space": "YUV",
      "chroma_subsampling": "4:2:0",
      "other_chroma_subsampling": [
        "4:2:0"
      ],
      "bit_depth": 8,
      "other_bit_depth": [
        "8 bits"
      ],
      "scan_type": "Progressive",
      "other_scan_type": [
        "Progressive"
      ],
      "interlacement": "PPF",
      "other_interlacement": [
        "Progressive"
      ],
      "delay": "1481.000",
      "other_delay": [
        "1 s 481 ms",
        "1 s 481 ms",
        "1 s 481 ms",
        "00:00:01.481"
      ],
      "delay__origin": "Container",
      "other_delay__origin": [
        "Container"
      ],
      "writing_library": "x264 - core 148",
      "other_writing_library": [
        "x264 core 148"
      ],
      "encoded_library_name": "x264",
      "encoded_library_version": "core 148",
      "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"
    },
    {
      "track_type": "Audio",
      "count": "275",
      "count_of_stream_of_this_kind": "1",
      "kind_of_stream": "Audio",
      "other_kind_of_stream": [
        "Audio"
      ],
      "stream_identifier": "0",
      "streamorder": "0-1",
      "track_id": 257,
      "other_track_id": [
        "257 (0x101)"
      ],
      "menu_id": 1,
      "other_menu_id": [
        "1 (0x1)"
      ],
      "format": "AAC",
      "format_info": "Advanced Audio Codec",
      "commercial_name": "AAC",
      "format_version": "Version 4",
      "format_profile": "LC",
      "muxing_mode": "ADTS",
      "codec_id": "15",
      "codec": "AAC LC",
      "other_codec": [
        "AAC LC"
      ],
      "duration": 31093,
      "other_duration": [
        "31 s 93 ms",
        "31 s 93 ms",
        "31 s 93 ms",
        "00:00:31.093",
        "00:00:31.093"
      ],
      "bit_rate_mode": "VBR",
      "other_bit_rate_mode": [
        "Variable"
      ],
      "channel_s": 2,
      "other_channel_s": [
        "2 channels"
      ],
      "channel_positions": "Front: L R",
      "other_channel_positions": [
        "2/0/0"
      ],
      "channellayout": "L R",
      "samples_per_frame": "1024",
      "sampling_rate": 44100,
      "other_sampling_rate": [
        "44.1 kHz"
      ],
      "samples_count": "1371201",
      "frame_rate": "43.066",
      "other_frame_rate": [
        "43.066 FPS (1024 SPF)"
      ],
      "compression_mode": "Lossy",
      "other_compression_mode": [
        "Lossy"
      ],
      "delay": "1400.000",
      "other_delay": [
        "1 s 400 ms",
        "1 s 400 ms",
        "1 s 400 ms",
        "00:00:01.400"
      ],
      "delay__origin": "Container",
      "other_delay__origin": [
        "Container"
      ],
      "delay_relative_to_video": -81,
      "other_delay_relative_to_video": [
        "-81 ms",
        "-81 ms",
        "-81 ms",
        "-00:00:00.081"
      ],
      "video0_delay": -81,
      "other_video0_delay": [
        "-81 ms",
        "-81 ms",
        "-81 ms",
        "-00:00:00.081"
      ]
    },
    {
      "track_type": "Menu",
      "count": "94",
      "count_of_stream_of_this_kind": "1",
      "kind_of_stream": "Menu",
      "other_kind_of_stream": [
        "Menu"
      ],
      "stream_identifier": "0",
      "streamorder": "0",
      "track_id": 4096,
      "other_track_id": [
        "4096 (0x1000)"
      ],
      "menu_id": 1,
      "other_menu_id": [
        "1 (0x1)"
      ],
      "format": "AVC / AAC",
      "commercial_name": "AVC / AAC",
      "codec": "AVC / AAC LC",
      "other_codec": [
        "AVC / AAC LC"
      ],
      "duration": "31240.000000",
      "other_duration": [
        "31 s 240 ms",
        "31 s 240 ms",
        "31 s 240 ms",
        "00:00:31.240",
        "00:00:31.240"
      ],
      "delay": "1401.000000",
      "other_delay": [
        "1 s 401 ms",
        "1 s 401 ms",
        "1 s 401 ms",
        "00:00:01.401"
      ],
      "list_streamkind": "1 / 2",
      "list_streampos": "0 / 0",
      "list": "256 / 257",
      "other_list": [
        "256 (0x100) (AVC) / 257 (0x101) (AAC)"
      ],
      "service_name": "Service01",
      "service_provider": "FFmpeg",
      "service_type": "digital television",
      "pointer_field": "0",
      "section_length": "23"
    }
  ]
}

 

2人点赞

 

新媒体管家发布

 

"小礼物走一走,来简书关注我"



作者:测试游记
链接:https://www.jianshu.com/p/4c115bd82774
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

展开阅读全文
©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读