Docker化FFmpeg无理由停止问题排查求助
容器中FFmpeg读取RTSP流后无报错停止的问题
我尝试用Docker容器运行FFmpeg,读取rtsp://192.168.1.85:8554/camera流并保存为30秒分段MP4,但容器启动约20秒后无报错就停止了。
Dockerfile内容
FROM linuxserver/ffmpeg ENTRYPOINT ffmpeg -i rtsp://192.168.1.85:8554/camera -f v4l2 -c copy -reset_timestamps 1 -map 0 -f segment -segment_time 30 -segment_format mp4 "output/out%03d.mp4" -loglevel debug
运行命令
sudo docker run -it --rm -v /data/camera:/output --name camera_recorder camera_recorder:latest
容器运行输出
[+] Building 1.8s (5/5) FINISHED => [internal] load build definition from Dockerfile 0.3s => => transferring dockerfile: 777B 0.0s => [internal] load .dockerignore 0.5s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/linuxserver/ffmpeg:latest 1.2s => CACHED [1/1] FROM docker.io/linuxserver/ffmpeg@sha256:823c611e0af82b864608c21d96bf363403310d92f154e238f6d51fe3d783e53b 0.0s => exporting to image 0.1s => => exporting layers 0.0s => => writing image sha256:f0509ccf0b07ff53d4aafa0d3b80fd50ed53e96db906c9a1e0e8c44e163dce94 0.1s => => naming to docker.io/library/camera_recorder 0.0s ffmpeg version 5.1.2 Copyright (c) 2000-2022 the FFmpeg developers built with gcc 11 (Ubuntu 11.3.0-1ubuntu1~22.04) configuration: --disable-debug --disable-doc --disable-ffplay --enable-ffprobe --enable-cuvid --enable-gpl --enable-libaom --enable-libass --enable-libfdk_aac --enable-libfreetype --enable-libkvazaar --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libxml2 --enable-libx264 --enable-libx265 --enable-libxvid --enable-nonfree --enable-nvdec --enable-nvenc --enable-opencl --enable-openssl --enable-small --enable-stripping --enable-vaapi --enable-vdpau --enable-version3 libavutil 57. 28.100 / 57. 28.100 libavcodec 59. 37.100 / 59. 37.100 libavformat 59. 27.100 / 59. 27.100 libavdevice 59. 7.100 / 59. 7.100 libavfilter 8. 44.100 / 8. 44.100 libswscale 6. 7.100 / 6. 7.100 libswresample 4. 7.100 / 4. 7.100 libpostproc 56. 6.100 / 56. 6.100 Splitting the commandline. Reading option '-i' ... matched as input url with argument 'rtsp://192.168.1.85:8554/camera'. Reading option '-f' ... matched as option 'f' (force format) with argument 'v4l2'. Reading option '-c' ... matched as option 'c' (codec name) with argument 'copy'. Reading option '-reset_timestamps' ... matched as AVOption 'reset_timestamps' with argument '1'. Reading option '-map' ... matched as option 'map' (set input stream mapping) with argument '0'. Reading option '-f' ... matched as option 'f' (force format) with argument 'segment'. Reading option '-segment_time' ... matched as AVOption 'segment_time' with argument '30'. Reading option '-segment_format' ... matched as AVOption 'segment_format' with argument 'mp4'. Reading option 'output/out%03d.mp4' ... matched as output url. Reading option '-loglevel' ... matched as option 'loglevel' (set logging level) with argument 'debug'. Finished splitting the commandline. Parsing a group of options: global . Applying option loglevel (set logging level) with argument debug. Successfully parsed a group of options. Parsing a group of options: input url rtsp://192.168.1.85:8554/camera. Successfully parsed a group of options. Opening an input file: rtsp://192.168.1.85:8554/camera. [tcp @ 0x55c15e3eb040] No default whitelist set [tcp @ 0x55c15e3eb040] Original list of addresses: [tcp @ 0x55c15e3eb040] Address 192.168.1.85 port 8554 [tcp @ 0x55c15e3eb040] Interleaved list of addresses: [tcp @ 0x55c15e3eb040] Address 192.168.1.85 port 8554 [tcp @ 0x55c15e3eb040] Starting connection attempt to 192.168.1.85 port 8554 [tcp @ 0x55c15e3eb040] Successfully connected to 192.168.1.85 port 8554 [rtsp @ 0x55c15e3e8300] SDP: v=0 o=- 0 0 IN IP4 127.0.0.1 s=Stream c=IN IP4 0.0.0.0 t=0 0 m=video 0 RTP/AVP 96 a=control:rtsp://192.168.1.85:8554/camera/trackID=0 a=rtpmap:96 MP4V-ES/90000 a=fmtp:96 config=000001B001000001B58913000001000000012000C48D88002D3C04871443000001B24C61766335392E33372E313030; profile-level-id=1 [rtsp @ 0x55c15e3e8300] video codec set to: mpeg4 [rtp @ 0x55c15e3ef600] No default whitelist set [udp @ 0x55c15e3f0200] No default whitelist set [udp @ 0x55c15e3f0200] end receive buffer size reported is 425984 [udp @ 0x55c15e3eff40] No default whitelist set [udp @ 0x55c15e3eff40] end receive buffer size reported is 425984 [rtsp @ 0x55c15e3e8300] setting jitter buffer size to 500 [rtsp @ 0x55c15e3e8300] hello state=0 [rtsp @ 0x55c15e3e8300] Could not find codec parameters for stream 0 (Video: mpeg4, 1 reference frame, none(left), 1920x1080 [SAR 1:1 DAR 16:9], 1/5): unspecified pixel format Consider increasing the value for the 'analyzeduration' (0) and 'probesize' (5000000) options Input #0, rtsp, from 'rtsp://192.168.1.85:8554/camera': Metadata: title : Stream Duration: N/A, bitrate: N/A Stream #0:0, 0, 1/90000: Video: mpeg4, 1 reference frame, none(left), 1920x1080 [SAR 1:1 DAR 16:9], 0/1, 5 tbr, 90k tbn Successfully opened the file. Parsing a group of options: output url output/out%03d.mp4. Applying option f (force format) with argument v4l2. Applying option c (codec name) with argument copy. Applying option map (set input stream mapping) with argument 0. Applying option f (force format) with argument segment. Successfully parsed a group of options. Opening an output file: output/out%03d.mp4. Successfully opened the file. [segment @ 0x55c15e415a80] Selected stream id:0 type:video [segment @ 0x55c15e415a80] Opening 'output/out000.mp4' for writing [file @ 0x55c15e42d840] Setting default whitelist 'file,crypto,data' Output #0, segment, to 'output/out%03d.mp4': Metadata: title : Stream encoder : Lavf59.27.100 Stream #0:0, 0, 1/10240: Video: mpeg4, 1 reference frame, none(left), 1920x1080 (0x0) [SAR 1:1 DAR 16:9], 0/1, q=2-31, 5 tbr, 10240 tbn Stream mapping: Stream #0:0 -> #0:0 (copy) Press [q] to stop, [?] for help cur_dts is invalid st:0 (0) [init:1 i_done:0 finish:0] (this is harmless if it occurs once at the start per stream) No more output streams to write to, finishing.:00.00 bitrate=N/A speed= 0x [segment @ 0x55c15e415a80] segment:'output/out000.mp4' count:0 ended [AVIOContext @ 0x55c15e42d8c0] Statistics: 292 bytes written, 2 seeks, 3 writeouts frame= 0 fps=0.0 q=-1.0 Lsize=N/A time=00:00:00.00 bitrate=N/A speed= 0x video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown Input file #0 (rtsp://192.168.1.85:8554/camera): Input stream #0:0 (video): 0 packets read (0 bytes); Total: 0 packets (0 bytes) demuxed Output file #0 (output/out%03d.mp4): Output stream #0:0 (video): 0 packets muxed (0 bytes); Total: 0 packets (0 bytes) muxed 0 frames successfully decoded, 0 decoding errors
补充信息
- RTSP流正常,执行
ffplay rtsp://192.168.1.85:8554/camera可正常播放 - 在宿主机执行容器ENTRYPOINT中的相同命令,可正常生成30秒分段文件
- 容器内能ping通192.168.1.85(该地址实际为localhost)
- 设置
-analyzeduration 1000无法解决问题
问题原因分析
从输出日志可以看到,FFmpeg虽然成功连接到RTSP服务器,但没有读取到任何数据包(0 packets read),最终因为没有输出内容而退出。核心问题出在FFmpeg命令中的-f v4l2参数:
这个参数是强制将输入格式设置为v4l2(视频采集设备格式),但输入是RTSP流,v4l2格式并不适配RTSP协议。宿主机上命令能运行可能是因为FFmpeg自动忽略了无效的输入格式参数,或者宿主机的FFmpeg配置有兼容处理,但容器内的FFmpeg严格执行了格式要求,导致无法解析RTSP流的数据包,最终无数据输出而停止。
解决方案
修改Dockerfile中的ENTRYPOINT命令,移除多余的-f v4l2参数:
FROM linuxserver/ffmpeg ENTRYPOINT ffmpeg -i rtsp://192.168.1.85:8554/camera -c copy -reset_timestamps 1 -map 0 -f segment -segment_time 30 -segment_format mp4 "output/out%03d.mp4" -loglevel debug
重新构建镜像并运行容器,FFmpeg会自动识别RTSP输入格式,正常读取流并生成分段文件。
内容的提问来源于stack exchange,提问作者Arthur Attout
相关产品推荐
相关产品推荐

