V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Orciorc
V2EX  ›  程序员

ffmpeg 推流参数配置中关于输入格式(--input_format)问题

  •  
  •   Orciorc · 2021-03-07 22:19:45 +08:00 · 1270 次点击
    这是一个创建于 1359 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近在做一个关于视频推流的项目,使用到 ffmpeg 推流,推流命令如下:

    ffmpeg -i /dev/video0 -input_format mjpeg -s:v 1920x1080 -framerate 30 -f flv

    但 ffmpeg 完全无视了-input_format mjpeg,仍然使用 YUYV:

    Input #0, video4linux2,v4l2, from '/dev/video0':

    Duration: N/A, bitrate: 165888 kb/s

    Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 1920x1080, 165888 kb/s, 5 fps, 5 tbr, 1000k tbn, 1000k tbc

    之所以要用 mjpeg 格式,是因为我使用的 USB 摄像头只能在mjpg下使用1080p@30Hz

    pi@raspberrypi:~ $ v4l2-ctl --list-formats-ext -d /dev/video0 ioctl: VIDIOC_ENUM_FMT Type: Video Capture

    [0]: 'MJPG' (Motion-JPEG, compressed)
    	Size: Discrete 640x480
    		Interval: Discrete 0.033s (30.000 fps)
    		Interval: Discrete 0.040s (25.000 fps)
    		Interval: Discrete 0.033s (30.000 fps)
    		Interval: Discrete 0.040s (25.000 fps)
    	Size: Discrete 1920x1080
    		Interval: Discrete 0.033s (30.000 fps)
    		Interval: Discrete 0.040s (25.000 fps)
    	Size: Discrete 1280x960
    		Interval: Discrete 0.033s (30.000 fps)
    		Interval: Discrete 0.040s (25.000 fps)
    	Size: Discrete 1280x720
    		Interval: Discrete 0.033s (30.000 fps)
    		Interval: Discrete 0.040s (25.000 fps)
    	Size: Discrete 640x480
    		Interval: Discrete 0.033s (30.000 fps)
    		Interval: Discrete 0.040s (25.000 fps)
    		Interval: Discrete 0.033s (30.000 fps)
    		Interval: Discrete 0.040s (25.000 fps)
    [1]: 'YUYV' (YUYV 4:2:2)
    	Size: Discrete 640x480
    		Interval: Discrete 0.033s (30.000 fps)
    		Interval: Discrete 0.033s (30.000 fps)
    	Size: Discrete 1920x1080
    		Interval: Discrete 0.200s (5.000 fps)
    	Size: Discrete 1280x960
    		Interval: Discrete 0.200s (5.000 fps)
    	Size: Discrete 1280x720
    		Interval: Discrete 0.200s (5.000 fps)
    	Size: Discrete 640x480
    		Interval: Discrete 0.033s (30.000 fps)
    		Interval: Discrete 0.033s (30.000 fps)
    

    https://ffmpeg.org/ffmpeg-devices.html#Options-18 中提到

    input_format: Set the preferred pixel format (for raw video) or a codec name. This option allows one to select the input format, when several are available.

    请问如何强制 ffmpeg 使用 mjpg 格式输入呢?

    3 条回复    2021-03-07 22:59:32 +08:00
    ETiV
        1
    ETiV  
       2021-03-07 22:46:49 +08:00   ❤️ 1
    我很确定的是:ffmpeg 对参数顺序是有要求的 https://ffmpeg.org/ffmpeg.html

    > As a general rule, options are applied to the next specified file. Therefore, order is important, and you can have the same option on the command line multiple times. Each occurrence is then applied to the next input or output file. Exceptions from this rule are the global options (e.g. verbosity level), which should be specified first.


    你需要把能够影响到 -i /dev/video0 的参数们(比如 -input_format mjpeg )放到 -i 的前面
    ETiV
        2
    ETiV  
       2021-03-07 22:47:42 +08:00
    > 只能在 mjpg 下使用 1080p@30Hz

    我注意到了你加粗了这段,那就是这 3 个参数都要挪到 -i 前面 🤣
    Orciorc
        3
    Orciorc  
    OP
       2021-03-07 22:59:32 +08:00
    @ETiV #2 感谢!果真如此! 这真是一个神奇的 feature……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1006 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 21:45 · PVG 05:45 · LAX 13:45 · JFK 16:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.