FFmpeg多HTTP输出流实现求助:pipe与tee muxer方案均失效
FFmpeg多HTTP输出流问题解决方案
问题背景
需要实现FFmpeg多HTTP输出流功能,尝试两种方案均失败:
- Pipe方案:仅最后一个HTTP输出生效
- Tee Muxer方案:无法正常工作
尝试的失败命令
1. Pipe方案命令
ffmpeg -re -i sample1.mp3 -i sample2.mp3 -filter_complex amix=inputs=2 -f mp3 pipe:1 | ffmpeg -f mp3 -re -i pipe:0 -c copy -listen 1 -f mp3 http://127.0.0.1:4000 -c copy -listen 1 -f mp3 http://127.0.0.1:4010
2. Tee Muxer方案命令
ffmpeg -re -i sample1.mp3 -i sample2.mp3 -filter_complex amix=inputs=2 -c:a mp3 -f tee "[listen=1:f=mp3]http://127.0.0.1:4000|[listen=1:f=mp3]http://127.0.0.1:4010"
调试命令及日志
调试命令
ffmpeg -loglevel debug -hide_banner -re -i sample1.mp3 -filter_complex asplit=2 -c:a mp3 -listen 1 -f mp3 http://127.0.0.1:4000 -c:a mp3 -listen 1 -f mp3 http://127.0.0.1:4010
调试日志
Splitting the commandline. Reading option '-loglevel' ... matched as option 'loglevel' (set logging level) with argument 'debug'. Reading option '-hide_banner' ... matched as option 'hide_banner' (do not show program banner) with argument '1'. Reading option '-re' ... matched as option 're' (read input at native frame rate; equivalent to -readrate 1) with argument '1'. Reading option '-i' ... matched as input url with argument 'sample1.mp3'. Reading option '-filter_complex' ... matched as option 'filter_complex' (create a complex filtergraph) with argument 'asplit=2'. Reading option '-c:a' ... matched as option 'c' (codec name) with argument 'mp3'. Reading option '-listen' ... matched as AVOption 'listen' with argument '1'. Reading option '-f' ... matched as option 'f' (force format) with argument 'mp3'. Reading option 'http://127.0.0.1:4000' ... matched as output url. Reading option '-c:a' ... matched as option 'c' (codec name) with argument 'mp3'. Reading option '-listen' ... matched as AVOption 'listen' with argument '1'. Reading option '-f' ... matched as option 'f' (force format) with argument 'mp3'. Reading option 'http://127.0.0.1:4010' ... matched as output url. Finished splitting the commandline. Parsing a group of options: global . Applying option loglevel (set logging level) with argument debug. Applying option hide_banner (do not show program banner) with argument 1. Applying option filter_complex (create a complex filtergraph) with argument 'asplit=2'. Successfully parsed a group of options. Parsing a group of options: input url sample1.mp3. Applying option re (read input at native frame rate; equivalent to -readrate 1) with argument '1'. Successfully parsed a group of options. Opening an input file: sample1.mp3. [NULL @ 00000216ab345b80] Opening 'sample1.mp3' for reading [file @ 00000216ab346180] Setting default whitelist 'file,crypto,data' [mp3 @ 00000216ab345b80] Format mp3 probed with size=4096 and score=51 id3v2 ver:4 flags:00 len:134 [mp3 @ 00000216ab345b80] pad 576 576 [mp3 @ 00000216ab345b80] Skipping 0 bytes of junk at 561. [mp3 @ 00000216ab345b80] Before avformat_find_stream_info() pos: 561 bytes read:32768 seeks:0 nb_streams:1 [mp3 @ 00000216ab345b80] demuxer injecting skip 1105 / discard 0 [mp3float @ 00000216ab34f3c0] skip 1105 / discard 0 samples due to side data [mp3float @ 00000216ab34f3c0] skip 1105/1152 samples [mp3 @ 00000216ab345b80] All info found [mp3 @ 00000216ab345b80] After avformat_find_stream_info() pos: 22065 bytes read:32768 seeks:0 frames:50 Input #0, mp3, from 'sample1.mp3': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2mp41 encoder : Lavf58.45.100 Duration: 00:12:58.48, start: 0.025057, bitrate: 128 kb/s Stream #0:0, 50, 1/14112000: Audio: mp3, 44100 Hz, stereo, fltp, 128 kb/s Metadata: encoder : Lavc58.91 Successfully opened the file. [Parsed_asplit_0 @ 00000216ab34fc80] Setting 'outputs' to value '2' Parsing a group of options: output url http://127.0.0.1:4000. Applying option c:a (codec name) with argument mp3. Applying option f (force format) with argument mp3. Successfully parsed a group of options. Opening an output file: http://127.0.0.1:4000. Matched encoder 'libmp3lame' for codec 'mp3'. Last message repeated 1 times [http @ 00000216ab44d980] Setting default whitelist 'http,https,tls,rtp,tcp,udp,crypto,httpproxy,data'
可行解决方案
方案1:使用asplit滤镜+多独立HTTP输出
先通过amix混合两个音频输入,再用asplit将混合后的流拆分为两路,为每个HTTP输出单独指定-listen 1参数:
ffmpeg -re -i sample1.mp3 -i sample2.mp3 -filter_complex "amix=inputs=2,asplit=2" -c:a mp3 -listen 1 -f mp3 http://127.0.0.1:4000 -listen 1 -f mp3 http://127.0.0.1:4010
原方案失败原因:Pipe方案中,第二个FFmpeg进程的多个HTTP输出共享同一管道输入,只有最后一个输出能正常进入监听状态,其余输出会因流式输入的阻塞特性无法工作。
方案2:修正Tee Muxer命令格式
Tee Muxer要求每个输出的参数(包括listen)需放在对应输出的方括号内,且需明确指定格式协议:
ffmpeg -re -i sample1.mp3 -i sample2.mp3 -filter_complex amix=inputs=2 -c:a mp3 -f tee "[f=mp3:listen=1]http://127.0.0.1:4000|[f=mp3:listen=1]http://127.0.0.1:4010"
原方案失败原因:原Tee命令参数顺序错误,listen是HTTP协议选项,需与格式参数f=mp3一同放在对应输出的配置块中,而非全局指定。
内容的提问来源于stack exchange,提问作者Caner Demir
相关产品推荐
相关产品推荐

