目前在做直播系统,其中有一项转播功能,需要自建拉转推的服务,目前我使用ffmpeg来转流,在后台界面操作后php程序调用shell指令运行ffmpeg进程,配合进程监控脚本来控制转播的启动、停止和维护。
其中主要的一条指令如下:
nohup ffmpeg -i 源流地址 -c:v copy -c:v copy -f flv 推流地址 >> /dev/null 2>&1 &
但是一旦末尾加了&
符到后台运行,就会卡在如下位置,而进程依旧在运行:
ffmpeg version 4.1.1 Copyright (c) 2000-2019 the FFmpeg developers
built with Apple LLVM version 10.0.0 (clang-1000.11.45.5)
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.1 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
libavutil 56. 22.100 / 56. 22.100
libavcodec 58. 35.100 / 58. 35.100
libavformat 58. 20.100 / 58. 20.100
libavdevice 58. 5.100 / 58. 5.100
libavfilter 7. 40.101 / 7. 40.101
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 3.100 / 5. 3.100
libswresample 3. 3.100 / 3. 3.100
libpostproc 55. 3.100 / 55. 3.100
而不使用后台运行则可以正常运行:
ffmpeg version 4.1.1 Copyright (c) 2000-2019 the FFmpeg developers
built with Apple LLVM version 10.0.0 (clang-1000.11.45.5)
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.1 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
libavutil 56. 22.100 / 56. 22.100
libavcodec 58. 35.100 / 58. 35.100
libavformat 58. 20.100 / 58. 20.100
libavdevice 58. 5.100 / 58. 5.100
libavfilter 7. 40.101 / 7. 40.101
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 3.100 / 5. 3.100
libswresample 3. 3.100 / 3. 3.100
libpostproc 55. 3.100 / 55. 3.100
[hls,applehttp @ 0x7f8b08009a00] Opening 'https://play.live.huanqiu.com/live/Vyb82mev-1598268314.ts' for reading
[hls,applehttp @ 0x7f8b08009a00] Opening 'https://play.live.huanqiu.com/live/Vyb82mev-1598268315.ts' for reading
Input #0, hls,applehttp, from 'https://play.live.huanqiu.com/live/Vyb82mev.m3u8?txSecret=3d6a537131a08c551f3f6739317cbcab&txTime=5F43D202':
Duration: N/A, start: 3068.078478, bitrate: N/A
Program 0
Metadata:
variant_bitrate : 0
Stream #0:0: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 860x538, 30 tbr, 90k tbn, 60 tbc
Metadata:
variant_bitrate : 0
Stream #0:1: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp
Metadata:
variant_bitrate : 0
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (aac (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, flv, to 'rtmp://live-push.bilivideo.com/live-bvc/?streamname=live_19264294_2672989&key=91beb80870d23c2f82662d0376ee80c8':
Metadata:
encoder : Lavf58.20.100
Stream #0:0: Video: h264 (High) ([7][0][0][0] / 0x0007), yuv420p, 860x538, q=2-31, 30 tbr, 1k tbn, 90k tbc
Metadata:
variant_bitrate : 0
Stream #0:1: Audio: mp3 (libmp3lame) ([2][0][0][0] / 0x0002), 44100 Hz, stereo, fltp
Metadata:
variant_bitrate : 0
encoder : Lavc58.35.100 libmp3lame
[https @ 0x7f8b0801cc00] Opening 'https://play.live.huanqiu.com/live/Vyb82mev-1598268316.ts' for reading
[https @ 0x7f8b0801cc00] Opening 'https://play.live.huanqiu.com/live/Vyb82mev-1598268317.ts' for reading
[https @ 0x7f8b07852000] Opening 'https://play.live.huanqiu.com/live/Vyb82mev.m3u8?txSecret=3d6a537131a08c551f3f6739317cbcab&txTime=5F43D202' for reading
[https @ 0x7f8b0801cc00] Opening 'https://play.live.huanqiu.com/live/Vyb82mev-1598268318.ts' for reading
[hls,applehttp @ 0x7f8b08009a00] Opening 'https://play.live.huanqiu.com/live/Vyb82mev-1598268318.ts' for reading
[https @ 0x7f8b0803c000] Opening 'https://play.live.huanqiu.com/live/Vyb82mev.m3u8?txSecret=3d6a537131a08c551f3f6739317cbcab&txTime=5F43D202' for reading
[https @ 0x7f8b0701c800] Opening 'https://play.live.huanqiu.com/live/Vyb82mev-1598268319.ts' for reading
[hls,applehttp @ 0x7f8b08009a00] Opening 'https://play.live.huanqiu.com/live/Vyb82mev-1598268319.ts' for reading
[https @ 0x7f8b07043600] Opening 'https://play.live.huanqiu.com/live/Vyb82mev.m3u8?txSecret=3d6a537131a08c551f3f6739317cbcab&txTime=5F43D202' for reading
当你运行一个进程时,它有3个打开的管道: stdin
, stdout
和 stderr
.管道有一个内存缓冲区(Linux上为4KB),可以容纳一定数量的数据,下一个写操作将暂停,直到从管道的另一侧读取一些数据。
ffmpeg
默认情况下启用与 stdin
的交互。在Mac OS X和Linux系统上,这会导致 ffmpeg
在后台运行的作业挂起。
-nostdin
选项会导致ffmpeg无法启用stdin交互,因此避免了挂起后台进程。nohup ffmpeg -nostdin -i 源流地址 -c:v copy -c:v copy -f flv 推流地址 >> /dev/null 2>&1 &
nohup ffmpeg -i 源流地址 -c:v copy -c:v copy -f flv 推流地址 >> /dev/null 2>&1
如果对您有帮助不妨点赞关注一波~