如何使用ffprobe检测HLS/M3U8文件中的视频比特率

10

我们需要使用ffprobe,通过使用m3u8文件中的.ts来检测HLS流的视频比特率

如果我使用m3u8,可以获得持续时间、尺寸、使用的编解码器、音频比特率,但在ffprobe提供的响应中没有可用的视频比特率

ffprobe -print_format json -show_format -show_streams -show_error http://gfrmedia-video-platform.s3.amazonaws.com/bumbia/2014/06/06/158217_20160126214307_bumbia-hls/hls1056k/158217_640x360-with-mp4-hls_bumbia-hls.m3u8

ffprobe version 2.8.3 Copyright (c) 2007-2015 the FFmpeg developers
  built with Apple LLVM version 7.0.0 (clang-700.1.76)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/2.8.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-opencl --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc --enable-libxvid --enable-vda
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
{
[http @ 0x7fcf09e19420] No trailing CRLF found in HTTP header.
Input #0, hls,applehttp, from 'http://gfrmedia-video-platform.s3.amazonaws.com/bumbia/2014/06/06/158217_20160126214307_bumbia-hls/hls1056k/158217_640x360-with-mp4-hls_bumbia-hls.m3u8':
  Duration: 00:00:11.00, start: 9.940500, bitrate: 0 kb/s
  Program 0
    Metadata:
      variant_bitrate : 0
    Stream #0:0: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p, 640x360 [SAR 1:1 DAR 16:9], 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
    Stream #0:1: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 79 kb/s
    "streams": [
        {
            "index": 0,
            "codec_name": "h264",
            "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
            "profile": "Constrained Baseline",
            "codec_type": "video",
            "codec_time_base": "1001/60000",
            "codec_tag_string": "[27][0][0][0]",
            "codec_tag": "0x001b",
            "width": 640,
            "height": 360,
            "coded_width": 640,
            "coded_height": 368,
            "has_b_frames": 0,
            "sample_aspect_ratio": "1:1",
            "display_aspect_ratio": "16:9",
            "pix_fmt": "yuv420p",
            "level": 30,
            "chroma_location": "left",
            "refs": 1,
            "is_avc": "0",
            "nal_length_size": "0",
            "r_frame_rate": "30000/1001",
            "avg_frame_rate": "30000/1001",
            "time_base": "1/90000",
            "start_pts": 900000,
            "start_time": "10.000000",
            "bits_per_raw_sample": "8",
            "disposition": {
                "default": 0,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0
            }
        },
        {
            "index": 1,
            "codec_name": "aac",
            "codec_long_name": "AAC (Advanced Audio Coding)",
            "profile": "LC",
            "codec_type": "audio",
            "codec_time_base": "1/44100",
            "codec_tag_string": "[15][0][0][0]",
            "codec_tag": "0x000f",
            "sample_fmt": "fltp",
            "sample_rate": "44100",
            "channels": 2,
            "channel_layout": "stereo",
            "bits_per_sample": 0,
            "r_frame_rate": "0/0",
            "avg_frame_rate": "0/0",
            "time_base": "1/90000",
            "start_pts": 894645,
            "start_time": "9.940500",
            "bit_rate": "79931",
            "disposition": {
                "default": 0,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0
            }
        }
    ],
    "format": {
        "filename": "http://gfrmedia-video-platform.s3.amazonaws.com/bumbia/2014/06/06/158217_20160126214307_bumbia-hls/hls1056k/158217_640x360-with-mp4-hls_bumbia-hls.m3u8",
        "nb_streams": 2,
        "nb_programs": 1,
        "format_name": "hls,applehttp",
        "format_long_name": "Apple HTTP Live Streaming",
        "start_time": "9.940500",
        "duration": "11.000000",
        "size": "281",
        "bit_rate": "204",
        "probe_score": 100
    }
}
在这个示例中,我知道视频大约是520Kbps,但需要找到一种用ffprobe或其他方式程序化读取的方法。使用当前已有的信息可以获取大部分所需信息,但我缺少一个重要的值,即视频比特率

我还可以尝试哪些其他选项?

谢谢!


这可能有点繁琐,但您可以将所有.ts文件的大小(以比特为单位)相加,然后除以持续时间(以秒为单位)。 - Matej Ukmar
3个回答

3

对于视频比特率,您可以进行以下操作:

ffprobe -select_streams v:0 -show_entries frame=pkt_size,pkt_duration_time <input>

获取一系列帧的压缩数据包大小和持续时间(以字节和秒为单位)。运行一段时间后,计算总持续时间的平均比特率。


3

检测平均带宽的最佳方法是将ffmpeg和pv命令结合使用,如下所示:

ffmpeg  -re -i input.m3u8 -f flv - 2>/dev/null|pv -a >/dev/null

您可能需要安装 pv apt-get install pv 希望它对您有帮助。


1
在上面的示例中,我知道视频大约是520Kbps,但需要找到一种方法通过ffprobe或其他方式以编程方式读取它。
您显示的HLS文件中的文本有一个条目:AVERAGE-BANDWIDTH:599686
FFProbe说AAC音频比特率是:79 kb/s 要获取视频比特率,请使用以下公式: HLS平均带宽减去FFProbe音频比特率 = 估计的视频比特率
根据您的数字...(599686-79000)= 520686。
这实际上确认了您的“我知道视频大约是520Kbps”。只需将HLS文本文件转换为String并使用一些字符串函数(甚至正则表达式)提取第一个数字即可。
希望能对您有所帮助。

你从哪里得到了 599686?谢谢。 - danday74
@danday74 这是来自Asker提供的m3u8文本文件(http://gfrmedia-video-platform.s3.amazonaws.com/bumbia/2014/06/06/158217_20160126214307_bumbia-hls/hls1056k/158217_640x360-with-mp4-hls_bumbia-hls.m3u8)。 - VC.One

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接