java - 如何在不阻塞第一个输入的情况下运行命令行 FFMPEG 并接受多个管道(视频和音频)?

标签 java android linux video ffmpeg

我正在尝试使用 FFMPEG 混合使用 MediaCodec 创建的 h264 和 aac,并且还使用 FFMPEG 的 RTMP 支持发送到 youtube。我创建了两个管道,并通过 WriteableByteChannels 从 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""

管道是用 mkfifo 创建的,并像这样从 java 打开:

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

执行顺序(目前在我的测试阶段)是创建文件,启动 ffmpeg(通过 adb shell),然后开始录制打开 channel 。 ffmpeg 将立即打开 h264 流然后等待,因为它正在从管道读取第一个打开的 channel (用于视频)将成功运行。当试图以同样的方式打开音频时,它失败了,因为 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();

谢谢。

最佳答案

您的解释不是很清楚,我可以看出您正试图准确解释您在做什么,但没有用。

首先:您能否描述实际问题。我必须读到你的文章中途变成一个 8 行的段落,看起来你是在暗示 ffmpeg 挂起。那是问题吗?你真的想明确说明这一点。

其次:如何将数据通过管道传输到 FIFO 中?这很重要。您的帖子完全不清楚,您似乎建议 ffmpeg 读取整个视频文件然后移动到音频。那是对的吗?还是两个流都同时馈送到 ffmpeg?

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

关于java - 如何在不阻塞第一个输入的情况下运行命令行 FFMPEG 并接受多个管道(视频和音频)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35469445/

相关文章:

java - 同步:ImageDownloader线程

java.lang.NoClassDefFoundError : net/sf/cglib/asm/util/TraceClassVisitor

android - 两个应用程序的相同 Android 服务实例

javascript - 将 HTML5 应用程序转换为独立的 Android 应用程序

regex - 如何检查一个字符串是否至少包含一个字母字符?

linux - (Linux) 如何让子进程的子进程等待

java - 如何将自定义命名空间添加到 JSF 中的 HTML 元素?

java - 与 Springs @ModelAttribute 注释的命名约定混淆

java - ListVIew 未显示在我的布局、Android Studio 中

linux - 允许 aa-exec 临时用户访问文件夹