当前位置: 首页 > 知识库问答 >
问题:

如何运行命令行FFMPEG并接受多个管道(视频和音频),而不阻塞第一个输入?

孙嘉悦
2023-03-14

我正在尝试使用FFMPEG对使用MediaCodec创建的h264和aac进行多路复用,并使用FFMPEG的RTMP支持发送到youtube。我创建了两个管道,并通过可写字节通道从java(android)编写。我可以这样发送到一个管道(接受空音频):

./ffmpeg -f lavfi -i aevalsrc=0 -i "files/camera-test.h264" -acodec aac -vcodec copy -bufsize 512k -f flv "rtmp://a.rtmp.youtube.com/live2/XXXX"

YouTube流媒体效果很好(但我没有音频)。使用两个管道这是我的命令:

./ffmpeg \
-i "files/camera-test.h264" \
-i "files/audio-test.aac" \
-vcodec copy \
-acodec copy \
-map 0:v:0 -map 1:a:0 \
-f flv "rtmp://a.rtmp.youtube.com/live2/XXXX""

这些管道是使用mkfiro创建的,并从java打开,如下所示:

pipeWriterVideo = Channels.newChannel(new FileOutputStream(outputFileVideo.toString()));

执行顺序(目前在我的测试阶段)是创建文件,启动ffmpeg(通过adb外壳),然后开始录制,打开通道。ffmpeg将立即打开h264流,然后等待,因为它正在从管道读取第一个打开的通道(用于视频)将成功运行。当试图以同样的方式打开音频时,它失败了,因为ffmpeg实际上还没有开始从管道中读取。我可以打开第二个终端窗口,对音频文件进行cat,然后我的应用程序吐出我希望是编码的aac,但ffmpeg失败了,通常只是坐在那里等待。下面是详细的输出:

ffmpeg version N-78385-g855d9d2 Copyright (c) 2000-2016 the FFmpeg
developers
  built with gcc 4.8 (GCC)
  configuration: --prefix=/home/dev/svn/android-ffmpeg-with-rtmp/src/ffmpeg/android/arm 
    --enable-shared --disable-static --disable-doc --disable-ffplay 
    --disable-ffprobe --disable-ffserver --disable-symver 
    --cross-prefix=/home/dev/dev/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi- 
    --target-os=linux --arch=arm --enable-cross-compile 
    --enable-librtmp --enable-pic --enable-decoder=h264 
    --sysroot=/home/dev/dev/android-ndk-r10e/platforms/android-19/arch-arm 
    --extra-cflags='-Os -fpic -marm' 
    --extra-ldflags='-L/home/dev/svn/android-ffmpeg-with-rtmp/src/openssl-android/libs/armeabi ' 
    --extra-ldexeflags=-pie --pkg-config=/usr/bin/pkg-config
  libavutil      55. 17.100 / 55. 17.100
  libavcodec     57. 24.102 / 57. 24.102
  libavformat    57. 25.100 / 57. 25.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 31.100 /  6. 31.100
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
 matched as AVOption 'debug' with argument 'verbose'.
Trailing options were found on the commandline.
Finished splitting the commandline.
Parsing a group of options: global .
Applying option async (audio sync method) with argument 1.
Successfully parsed a group of options.
Parsing a group of options: input file files/camera-test.h264.
Successfully parsed a group of options.
Opening an input file: files/camera-test.h264.
[file @ 0xb503b100] Setting default whitelist 'file'

我想如果我能让ffmpeg开始听这两个管道,剩下的就行了!

谢谢你抽出时间。

编辑:我已经在分离音频管道连接和编码方面取得了进展,但现在视频流一经传递,就会在音频上出现错误。我启动了一个单独的线程来为音频创建WriteableByteChannel,但它从未通过FileOutputStream创建。

matched as AVOption 'debug' with argument 'verbose'.
Trailing options were found on the commandline.
Finished splitting the commandline.
Parsing a group of options: global .
Successfully parsed a group of options.
Parsing a group of options: input file files/camera-test.h264.
Successfully parsed a group of options.
Opening an input file: files/camera-test.h264.
[file @ 0xb503b100] Setting default whitelist 'file'
[h264 @ 0xb503c400] Format h264 probed with size=2048 and score=51
[h264 @ 0xb503c400] Before avformat_find_stream_info() pos: 0 bytes read:15719 seeks:0
[h264 @ 0xb5027400] Current profile doesn't provide more RBSP data in PPS, skipping
[h264 @ 0xb503c400] max_analyze_duration 5000000 reached at 5000000 microseconds st:0
[h264 @ 0xb503c400] After avformat_find_stream_info() pos: 545242 bytes read:546928 seeks:0 frames:127
Input #0, h264, from 'files/camera-test.h264':
  Duration: N/A, bitrate: N/A
    Stream #0:0, 127, 1/1200000: Video: h264 (Baseline), 1 reference frame, yuv420p(left), 854x480 (864x480), 1/50, 25 fps, 25 tbr, 1200k tbn, 50 tbc
Successfully opened the file.
Parsing a group of options: input file files/audio-test.aac.
Applying option vcodec (force video codec ('copy' to copy stream)) with argument copy.
Successfully parsed a group of options.
Opening an input file: files/audio-test.aac.
Unknown decoder 'copy'
[AVIOContext @ 0xb5054020] Statistics: 546928 bytes read, 0 seeks

这里是我尝试打开音频管道的地方。

new Thread(){
     public void run(){
          Log.d("Audio", "pre thread");
          FileOutputStream fs = null;
          try {
               fs = new FileOutputStream("/data/data/android.com.android.grafika/files/audio-test.aac");
          } catch (FileNotFoundException e) {
               e.printStackTrace();
          }
          Log.d("Audio", "made fileoutputstream");  //never hits here
          mVideoEncoder.pipeWriterAudio = Channels.newChannel(fs);
          Log.d("Audio", "made it past opening audio pipe");
     }
}.start();

谢谢

共有1个答案

慕弘伟
2023-03-14

你的解释不是很清楚,我可以看出你试图准确地解释你在做什么,但这不起作用。

第一:你能描述一下实际的问题吗。我得把你的帖子读到一半,变成一个8行的段落,看起来你是在暗示ffmpeg要挂了。这就是问题所在吗?你真的想把这件事说清楚。

第二:如何将数据导入FIFO?这很重要。你的帖子完全不清楚,你似乎建议ffmpeg读取整个视频文件,然后转到音频。对吗?或者两个流都同时馈送到ffmpeg?

最后:如果ffmpeg挂起,很可能是因为您的一个输入管道阻塞(您正在将数据推送到FIFO-1,缓冲区已满,但ffmpeg需要来自FIFO-2的数据,缓冲区为空)。两个FIFO都需要始终独立地填充数据。

 类似资料:
  • 我已经使用程序youtube-dl下载了一个YouTube播放列表,我选择了单独下载视频和音频,我现在有一个文件夹充满了视频及其相应的音频,我希望与ffmpeg合并在一起。 我需要使用批处理脚本来执行此操作,但问题是youtube-dl在原始文件的标题之后添加了临时字母,因此视频与其对应的音频没有相同的名称,文件名如下所示: 如何使用windows批处理脚本和ffmpeg合并这些多个视频/音频文件

  • 我有一个长音频部分和一个短视频部分,我想在一起mux。 我正在尝试使用以下命令进行MUX: video_0-0002.h264-整个文件(2秒长) Audio.wav-从4秒到6秒 但音频被搞砸了...我怎样才能正确地做呢? 也试过了,听起来好像最后还是有寂静。

  • 我正在尝试使用FFMPEG合并2个mp4文件。其中一个文件同时具有视频和音频(),而另一个只有音频()。这些文件的名称以以下方式列在名为的文本文件中: 然后执行下面的ffmpeg命令来合并它们。 但是,生成的连接文件只包含。也就是说,如果

  • 我有两个视频和两个音频 1:-视频1-长度:-60秒||音频1-长度:-15秒 2:-视频-长度:-86秒| |音频2-长度:-18秒 同时播放video1和audio1,如果audio1结束,则重新启动该音频,直到video1结束。这将是最后的视频1。 同样的方式, 同时播放video2和audio2,如果audio2结束,则重新启动该音频,直到video2结束。这将是最后的视频2。 之后,将最

  • 我试图转码一个单一的视频文件与1个视频流和几个音频流到文件具有相同的视频流在不同的比特率/大小与正确的填充在同一时间。 我使用的命令是: 实际上,对同一个文件进行几个比特率的代码转换是可能的吗?

  • 我最近制作了我的第一个discord机器人,我终于用下面的代码获得了通过机器人播放的音频。但是,如果与bot处于同一频道的人再次使用同一命令,bot将停止播放其音频并离开该频道。此外,我将如何使它,使机器人无法切换频道之前,它已经完成播放其音频。(注意:bot从包含mp3文件的URL播放音频,而不是使用YouTube插件,因为我只希望它为一些私人服务器播放特定内容)以下是代码: 非常感谢您的帮助。