使用 ffmpeg 降低帧率和比特率不起作用

Reduce framerate and bitrate with ffmpeg doesn't work

我正在使用 ffmpeg 在 rtsp 流上流式传输 raspberry PI cam。

在我将此命令与 OpenCV 结合使用之前:

ffmpeg -re -c:v copy -preset ultrafast -tune zerolatency -rtsp_transport udp -f rtsp rtsp_server

效果不错,但 CPU 使用率很高。

所以我发现用这个命令

ffmpeg -input_format h264 -i /dev/video0 -c:v copy -an -rtsp_transport udp -f rtsp rtsp_server

我没有CPU使用,但互联网上发送的数据量很大。

保存用最后一个命令捕获的视频并使用 mediainfo 获取它说的信息: 格式 : AVI Format/Info:音频视频交错 文件大小:2.52 MiB 持续时间:17 秒 783 毫秒 总比特率:1 188 kb/s 写应用:Lavf58.76.100

Video
ID                                       : 0
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4
Format settings                          : CABAC / 1 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 1 frame
Format settings, GOP                     : M=1, N=60
Codec ID                                 : H264
Duration                                 : 17 s 783 ms
Bit rate                                 : 1 174 kb/s
Width                                    : 640 pixels
Height                                   : 480 pixels
Display aspect ratio                     : 4:3
Frame rate                               : 60.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.064
Stream size                              : 2.49 MiB (99%)

我尝试使用此命令降低帧率和比特率:

ffmpeg -input_format h264 -i /dev/video0 -b:v 30k -framerate 30 -c:v copy -an video.avi

但是没用。

这是我保存视频时的ffmpeg输出:

ffmpeg -input_format h264 -i /dev/video0 -b:v 30k -framerate 30 -c:v copy -an video.avi
ffmpeg version a4e1dd6 Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 10 (Raspbian 10.2.1-6+rpi1)
  configuration: --extra-cflags='-I/usr/local/include -I/usr/include' --extra-ldflags='-L/usr/local/lib -L/usr/lib' --extra-libs='-lpthread -lm -latomic' --arch=armel --enable-gmp --enable-gpl --enable-libaom --enable-libass --enable-libdav1d --enable-libdrm --enable-libfdk-aac --enable-libfreetype --enable-libkvazaar --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr --enable-libssh --enable-libvorbis --enable-libvpx --enable-libzimg --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-mmal --enable-nonfree --enable-omx --enable-omx-rpi --enable-version3 --target-os=linux --enable-pthreads --enable-openssl --enable-hardcoded-tables
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, video4linux2,v4l2, from '/dev/video0':
  Duration: N/A, start: 14575.131904, bitrate: N/A
  Stream #0:0: Video: h264 (High), yuv420p(progressive), 640x480, 30 fps, 30 tbr, 1000k tbn, 2000k tbc
Output #0, avi, to 'video.avi':
  Metadata:
    ISFT            : Lavf58.76.100
  Stream #0:0: Video: h264 (High) (H264 / 0x34363248), yuv420p(progressive), 640x480, q=2-31, 30 kb/s, 30 fps, 30 tbr, 60 tbn, 60 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[avi @ 0x3044850] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly
frame=    1 fps=0.0 q=-1.0 size=      28kB time=00:00:00.01 bitrate=13675.9kbi[avi @ 0x3044850] Non-monotonous DTS in output stream 0:0; previous: 0, current: 0; changing to 1. This may result in incorrect timestamps in the output file.
frame=   23 fps=0.0 q=-1.0 size=     202kB time=00:00:00.71 bitrate=2305.9kbitframe=   38 fps= 37 q=-1.0 size=     256kB time=00:00:01.21 bitrate=1723.7kbitframe=   53 fps= 35 q=-1.0 size=     256kB time=00:00:01.71 bitrate=1221.6kbitframe=   69 fps= 34 q=-1.0 size=     512kB time=00:00:02.25 bitrate=1864.1kbitframe=   84 fps= 33 q=-1.0 size=     512kB time=00:00:02.75 bitrate=1525.2kbitframe=   99 fps= 33 q=-1.0 size=     768kB time=00:00:03.25 bitrate=1935.8kbitframe=  114 fps= 32 q=-1.0 size=     768kB time=00:00:03.75 bitrate=1677.7kbitframe=  129 fps= 32 q=-1.0 size=    1024kB time=00:00:04.25 bitrate=1973.8kbitframe=  144 fps= 32 q=-1.0 size=    1024kB time=00:00:04.75 bitrate=1766.0kbitframe=  159 fps= 31 q=-1.0 size=    1280kB time=00:00:05.25 bitrate=1997.3kbitframe=  175 fps= 31 q=-1.0 size=    1280kB time=00:00:05.78 bitrate=1813.1kbitframe=  190 fps= 31 q=-1.0 size=    1536kB time=00:00:06.28 bitrate=2002.6kbitframe=  205 fps= 31 q=-1.0 size=    1536kB time=00:00:06.78 bitrate=1855.0kbitframe=  220 fps= 31 q=-1.0 size=    1792kB time=00:00:07.28 bitrate=2015.6kbitframe=  235 fps= 31 q=-1.0 size=    1792kB time=00:00:07.78 bitrate=1886.1kbitframe=  251 fps= 31 q=-1.0 size=    2048kB time=00:00:08.31 bitrate=2017.3kbitframe=  266 fps= 31 q=-1.0 size=    2048kB time=00:00:08.81 bitrate=1902.9kbitframe=  279 fps= 31 q=-1.0 Lsize=    2315kB time=00:00:09.25 bitrate=2050.6kbits/s speed=1.02x    
video:2297kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.816921%

有人可以帮我减少输出大小吗?

解决了设置 v4l2 的最大比特率问题:

v4l2-ctl --set-ctrl video_bitrate=value_you_want