ffmpeg 挂起后台进程解决方法
当你运行一个进程时,它有 3 个打开的管道:stdin
,stdout
和stderr
。管道有一个内存缓冲区(Linux 上为 4 KB),可以容纳一定数量的数据,下一个写操作将暂停,直到从管道的另一侧读取一些数据。
ffmpeg
默认情况下启用与 stdin
的交互。在 Mac OS X 和 Linux 系统上,这会导致 ffmpeg
在后台运行的作业挂起。
-stdin
Enable interaction on standard input. On by default unless standard input is used as an input. To explicitly disable interaction you need to specify-nostdin
.
Disabling interaction on standard input is useful, for example, if ffmpeg is in the background process group. Roughly the same result can be achieved withffmpeg ... < /dev/null
but it requires a shell.
-stdin
启用标准输入交互。默认情况下打开,除非使用标准输入作为输入。要明确禁用交互,您需要指定-nostdin
。
禁用标准输入交互很有用,例如,如果 ffmpeg 在后台进程组中。使用ffmpeg ... < /dev/null
可以获得大致相同的结果,但它需要一个 shell。
案例演示
nohup ffmpeg -i "http://pull-hls-l11.douyincdn.com/third/stream-112056463879832028_or4.m3u8" -c copy -report "test.mp4" >> tset.log 2>&1 &
# 结果
[1] 46346
[1] + suspended (tty output) nohup ffmpeg -i -c copy -report "test.mp4" >> tset.log 2>&1
用pgrep -f -l ffmpeg
查看 ffmpeg 进程
46346 ffmpeg -i http://pull-hls-l11.douyincdn.com/third/stream-112056463879832028_or4.m3u8 -c copy -report test.mp4
从日志看 ffmpeg 进程已被暂停。
ffmpeg started on 2022-11-30 at 10:12:07
Report written to "ffmpeg-20221130-101207.log"
Log level: 48
ffmpeg version 5.1.2 Copyright (c) 2000-2022 the FFmpeg developers
built with Apple clang version 14.0.0 (clang-1400.0.29.102)
configuration: --prefix=/usr/local/Cellar/ffmpeg/5.1.2 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
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
进程已经挂起,ffmpeg 并没有正常运行。
添加 < /dev/null
后 ffmpeg 正常运行。
nohup ffmpeg -i "http://pull-hls-l11.douyincdn.com/third/stream-112056463879832028_or4.m3u8" -c copy -report "test.mp4" >> tset.log 2>&1 </dev/null &
# 结果
[2] 47243
ffmpeg 运行日志
ffmpeg version 5.1.2 Copyright (c) 2000-2022 the FFmpeg developers
built with Apple clang version 14.0.0 (clang-1400.0.29.102)
configuration: --prefix=/usr/local/Cellar/ffmpeg/5.1.2 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
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
[hls @ 0x7fcf132069c0] Skip ('#EXT-X-VERSION:3')
[hls @ 0x7fcf132069c0] Skip ('#EXT-X-ALLOW-CACHE:NO')
[hls @ 0x7fcf132069c0] Opening 'http://pull-hls-l11.douyincdn.com/third/stream-112056463879832028_or4-1669773166.ts?txspiseq=104016800799917470950' for reading
[hls @ 0x7fcf132069c0] Opening 'http://pull-hls-l11.douyincdn.com/third/stream-112056463879832028_or4-1669773167.ts?txspiseq=104016800799917470950' for reading
Input #0, hls, from 'http://pull-hls-l11.douyincdn.com/third/stream-112056463879832028_or4.m3u8':
Duration: N/A, start: 3025.400000, bitrate: N/A
Program 0
Metadata:
variant_bitrate : 0
Stream #0:0: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 1080x1920, 30 fps, 30 tbr, 90k tbn
Metadata:
variant_bitrate : 0
Stream #0:1: Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp
Metadata:
variant_bitrate : 0
Output #0, mp4, to 'test.mp4':
Metadata:
encoder : Lavf59.27.100
Stream #0:0: Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1080x1920, q=2-31, 30 fps, 30 tbr, 90k tbn
Metadata:
variant_bitrate : 0
Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp
Metadata:
variant_bitrate : 0
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 1 fps=0.0 q=-1.0 size= 0kB time=00:00:00.00 bitrate=34909.1kbits/s speed= 11x
[hls @ 0x7fcf132069c0] Opening 'http://pull-hls-l11.douyincdn.com/third/stream-112056463879832028_or4-1669773168.ts?txspiseq=104016800799917470950' for reading
[hls @ 0x7fcf132069c0] Skip ('#EXT-X-VERSION:3')
[hls @ 0x7fcf132069c0] Skip ('#EXT-X-ALLOW-CACHE:NO')
[hls @ 0x7fcf132069c0] Opening 'http://pull-hls-l11.douyincdn.com/third/stream-112056463879832028_or4-1669773169.ts?txspiseq=104016800799917470950' for reading
[hls @ 0x7fcf132069c0] Opening 'http://pull-hls-l11.douyincdn.com/third/stream-112056463879832028_or4-1669773170.ts?txspiseq=104016800799917470950' for reading
frame= 180 fps= 90 q=-1.0 size= 1536kB time=00:00:06.01 bitrate=2091.8kbits/s speed=3.01x
...
用pgrep -f -l ffmpeg
查看 ffmpeg 进程。
47243 ffmpeg -i http://pull-hls-l11.douyincdn.com/third/stream-112056463879832028_or4.m3u8 -c copy -report test.mp4
解决方法一
如果在指令中添加-nostdin
,选项会导致 ffmpeg 无法启用 stdin 交互,因此避免了挂起后台进程的问题。
nohup ffmpeg -nostdin -i 源流地址 -c:v copy -c:v copy -f flv 推流地址 >> /dev/null 2>&1 &
解决方法二
设置输入重定向</dev/null
nohup ffmpeg -i 源流地址 -c:v copy -c:v copy -f flv 推流地址 >> /dev/null 2>&1 < /dev/null &