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

MediaMuxer有时会导致黑色视频

姚文轩
2023-03-14

我正在使用Androidncoder生成一个带有AAC音频的H264视频,通过RTMP网络发送,并使用MediaMuxer本地存储在MP4文件中。

广播在RTMP上运行良好,但在本地保存时,生成的视频有时还可以,有时只是带有声音的黑色帧(或者只是带有一些信息块的第一部分帧),有时它先播放音频,然后以随机FPS速度播放视频。

这是我的Muxer包装器类和MPEG4Writer SDK类显示的输出:

02-28 11:57:38.521 6420-6475/com.myapp.broadcast W/AndroidMuxer: addTrack: Requested adding of track to class AndroidMuxer of type AUDIO
02-28 11:57:38.522 6420-6475/com.myapp.broadcast W/AndroidMuxer: addTrack: Adding track for audio/mp4a-latm
02-28 11:57:38.522 6420-6475/com.myapp.broadcast W/AndroidMuxer: addTrack: Track index to class AndroidMuxer of type AUDIO, track saved for future use!
02-28 11:57:38.621 6420-6470/com.myapp.broadcast W/AndroidMuxer: addTrack: Requested adding of track to class AndroidMuxer of type VIDEO
02-28 11:57:38.621 6420-6470/com.myapp.broadcast W/AndroidMuxer: addTrack: Adding track for video/avc
02-28 11:57:38.621 6420-6470/com.myapp.broadcast W/AndroidMuxer: addTrack: All tracks added, marking Local muxer as ready!
02-28 11:57:38.622 6420-6470/com.myapp.broadcast W/AndroidMuxer: addTrack: Track index to class AndroidMuxer of type VIDEO, track saved for future use!
02-28 11:57:52.050 6420-6420/com.myapp.broadcast I/AndroidMuxer: init: Added audio track widh id 0
02-28 11:57:52.050 6420-6420/com.myapp.broadcast I/AndroidMuxer: init: Added video track widh id 1
02-28 11:57:52.050 6420-6420/com.myapp.broadcast W/AndroidMuxer: init: Muxer was successfully created! Starting to record
02-28 11:57:52.056 6420-6420/com.myapp.broadcast I/MPEG4Writer: limits: 4294967295/0 bytes/us, bit rate: -1 bps and the estimated moov size 3195 bytes
02-28 11:57:52.057 6420-6420/com.myapp.broadcast W/AndroidMuxer: start: Muxer started!
02-28 11:57:52.070 6420-6965/com.myapp.broadcast I/MPEG4Writer: setStartTimestampUs: 0
02-28 11:57:52.070 6420-6965/com.myapp.broadcast I/MPEG4Writer: Earliest track starting time: 0
02-28 11:57:52.083 6420-6966/com.myapp.broadcast I/MPEG4Writer: setStartTimestampUs: 0
02-28 11:58:02.617 6420-6475/com.myapp.broadcast W/AndroidMuxer: writeSampleData: All tracks finished! Calling Stop
02-28 11:58:02.617 6420-6475/com.myapp.broadcast W/AndroidMuxer: stop: Calling stop to the muxer
02-28 11:58:02.617 6420-6475/com.myapp.broadcast D/MPEG4Writer: Audio track stopping
02-28 11:58:02.617 6420-6475/com.myapp.broadcast D/MPEG4Writer: Audio track source stopping
02-28 11:58:02.617 6420-6475/com.myapp.broadcast D/MPEG4Writer: Audio track source stopped
02-28 11:58:02.617 6420-6965/com.myapp.broadcast I/MPEG4Writer: Received total/0-length (452/0) buffers and encoded 452 frames. - Audio
02-28 11:58:02.617 6420-6965/com.myapp.broadcast I/MPEG4Writer: Audio track drift time: 0 us
02-28 11:58:02.617 6420-6475/com.myapp.broadcast D/MPEG4Writer: Audio track stopped
02-28 11:58:02.618 6420-6475/com.myapp.broadcast D/MPEG4Writer: Video track stopping
02-28 11:58:02.618 6420-6475/com.myapp.broadcast D/MPEG4Writer: Video track source stopping
02-28 11:58:02.618 6420-6475/com.myapp.broadcast D/MPEG4Writer: Video track source stopped
02-28 11:58:02.618 6420-6966/com.myapp.broadcast I/MPEG4Writer: Received total/0-length (315/0) buffers and encoded 315 frames. - Video
02-28 11:58:02.618 6420-6475/com.myapp.broadcast D/MPEG4Writer: Video track stopped
02-28 11:58:02.618 6420-6475/com.myapp.broadcast D/MPEG4Writer: Duration from tracks range is [10143021, 10495420] us
02-28 11:58:02.618 6420-6475/com.myapp.broadcast D/MPEG4Writer: Stopping writer thread
02-28 11:58:02.620 6420-6964/com.myapp.broadcast D/MPEG4Writer: 0 chunks are written in the last batch
02-28 11:58:02.620 6420-6475/com.myapp.broadcast D/MPEG4Writer: Writer thread stopped
02-28 11:58:02.621 6420-6475/com.myapp.broadcast I/MPEG4Writer: The mp4 file will not be streamable.
02-28 11:58:02.621 6420-6475/com.myapp.broadcast W/AndroidMuxer: stop: muxer stopped!

并不是说虽然本地保存的视频不好,但相同镜头的实时流(由相同的编码过程产生)很好,所以问题一定是在混音器本身(或者更准确地说,在我处理它的方式)。

这是我的Muxer包装(深受bigflake的mediacodec示例的启发):

import android.media.MediaCodec;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.util.Log;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * @hide
 */
public class AndroidMuxer extends Muxer {
    private static final String TAG = "AndroidMuxer";
    private static final boolean VERBOSE = false;

    private MediaMuxer mMuxer;
    private boolean mStarted = false;
    private boolean mStopped = false;
    private boolean mReadyToStart;
    private MediaFormat mVideoTrackFormat;
    private MediaFormat mAudioTrackFormat;

    private long mStartTimeUs = 0;
    @Override
    public void reset() {
        Log.w(TAG, "reset: Reset is called on LOCAL MUXER!");

            mStarted = false;
            mReadyToStart = false;
            mNumTracks = 0;
            mNumTracksFinished = 0;
            Log.w(TAG, "reset: Reset worked on the Muxer");

    }

    /**
     * This will only initialize the fields, we only want the real muxer to be initialized
     * when we go live.
     * @param outputFile outputPath of the file
     * @param format Format of the muxer (will always be MPEG4 TBH)
     */
    public AndroidMuxer(String outputFile, FORMAT format) {
        super(outputFile, format);
        mStarted = false;
    }

    @Override
    public void init() {
        if(!mReadyToStart) {
            return;
        }

        if(mStarted){
            Log.i(TAG, "init: Muxer already created! Skipping!");
            return;
        }


        try {
            switch (mFormat) {
                case MPEG4:
                    mMuxer = new MediaMuxer(mOutputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
                    int audioTrack = mMuxer.addTrack(mAudioTrackFormat);
                    Log.i(TAG, "init: Added audio track widh id " + audioTrack);
                    mAudioTrackIndex = audioTrack;

                    int videoTrack = mMuxer.addTrack(mVideoTrackFormat);
                    Log.i(TAG, "init: Added video track widh id " + videoTrack);
                    mVideoTrackIndex = videoTrack;
                    Log.w(TAG, "init: Muxer was successfully created! Starting to record");
                    start();
                    break;
                default:
                    throw new IllegalArgumentException("Unrecognized format!");
            }
        } catch (IOException e) {
            throw new RuntimeException("MediaMuxer creation failed", e);
        }
    }

    public static AndroidMuxer create(String outputFile, FORMAT format) {
        return new AndroidMuxer(outputFile, format);
    }

    @Override
    public int addTrack(MediaFormat trackFormat, AndroidEncoder.TrackType trackType) {
        Log.w(TAG, "addTrack: Requested adding of track to class " + getClass().getSimpleName() + " of type " + trackType);
        //Super method just keeps track of amount of tracks added
        super.addTrack(trackFormat, trackType);
        Log.w(TAG, "addTrack: Adding track for " + trackFormat.getString(MediaFormat.KEY_MIME));
        if (mStarted)
            throw new RuntimeException("format changed twice");

        //What we do is store the media info for later use once the muxer is created.
        //This is done to prevent file creation until the event goes live.

        if(trackType == AndroidEncoder.TrackType.VIDEO){
            mVideoTrackFormat = trackFormat;
        }
        else if(trackType == AndroidEncoder.TrackType.AUDIO){
            mAudioTrackFormat = trackFormat;
        }



        if (allTracksAdded()) {
            Log.w(TAG, "addTrack: All tracks added, marking Local muxer as ready!");
            markReadyToStart();
        }
        Log.w(TAG, "addTrack: Track index to class " + getClass().getSimpleName() + " of type " + trackType + ", track saved for future use!");
        //Return dummy
        return 1;
    }


    /**
     *
     * Marking the Muxer as ready to be started but not yet!
     *
     */
    private void markReadyToStart() {
        mReadyToStart = true;
    }



    private void start() {
        if(mStarted){
            Log.i(TAG, "start: Skipped start due to muxer already started");
        }
        mStartTimeUs = 0;
        mMuxer.start();
        Log.w(TAG, "start: Muxer started!");
        mStarted = true;
    }

    protected void stop() {
        if(mStarted) {
            Log.w(TAG, "stop: Calling stop to the muxer");
            mMuxer.stop();
            Log.w(TAG, "stop: muxer stopped!");
            mStarted = false;
            mStopped = true;
            mReadyToStart = false;
        }
    }

    @Override
    public void release() {
        super.release();
        if(mStopped) {
            mMuxer.release();
            mStopped = false;
            mStarted = false;
        }
    }

    @Override
    public boolean isStarted() {
        return mStarted;
    }

    @Override
    public void writeSampleData(MediaCodec encoder, int trackIndex, int bufferIndex, ByteBuffer encodedData, MediaCodec.BufferInfo bufferInfo) {
        if(!mStarted) {

            return;
        }

        super.writeSampleData(encoder, trackIndex, bufferIndex, encodedData, bufferInfo);
        if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
            // MediaMuxer gets the codec config info via the addTrack command
            if (VERBOSE) Log.d(TAG, "ignoring BUFFER_FLAG_CODEC_CONFIG");
            return;
        }

        if (bufferInfo.size == 0) {
            if (VERBOSE) Log.d(TAG, "ignoring zero size buffer");
            return;
        }

        if (!mStarted) {
            Log.w(TAG, "writeSampleData called before muxer started. Ignoring packet. Track index: " + trackIndex + " tracks added: " + mNumTracks);
//            encoder.releaseOutputBuffer(bufferIndex, false);
            return;
        }

//        bufferInfo.presentationTimeUs = getNextRelativePts(bufferInfo.presentationTimeUs, trackIndex);


       if(mStartTimeUs == 0) {
              mStartTimeUs = bufferInfo.presentationTimeUs;
             }
        bufferInfo.presentationTimeUs -= mStartTimeUs;
        if(bufferInfo.presentationTimeUs < 0) {
             bufferInfo.presentationTimeUs = 0;
        }
        bufferInfo.presentationTimeUs = getSafePts(bufferInfo.presentationTimeUs, trackIndex);


        mMuxer.writeSampleData(trackIndex, encodedData, bufferInfo);

//        encoder.releaseOutputBuffer(bufferIndex, false);

        if (allTracksFinished()) {
            Log.w(TAG, "writeSampleData: All tracks finished! Calling Stop");
            stop();
        }
    }

    @Override
    public void forceStop() {
        Log.w(TAG, "forceStop: ForceStop called!");
        stop();
    }
}

请注意,RTMP muxer和本地muxer可能不会同时启动,这就是为什么我存储媒体信息,然后仅在用户请求启动muxer时使用它。

共有1个答案

洪德寿
2023-03-14

如果本地muxer尚未准备好,而您跳过了写入数据包,则可能会跳过写入关键帧。在到达下一个关键帧之前,无法解码非关键帧的后续视频帧(如果文件以非关键帧开头,谁知道视频播放器是否会感到困惑?)。如果关键帧之间的间隔很长,则可能无法在合理的时间内获得关键帧。(不过,如果您也通过RTMP流式传输,可能会定期使用关键帧。)

我的建议是确保在本地混音器准备好之前存储所有生成的数据包,并在本地混音器准备好后将它们写入本地混音器,或者继续跳过数据包,直到您到达下一个关键帧。

 类似资料:
  • 问题内容: 我需要加快在应用程序中执行12个查询的速度。我从常规的foreach切换到Parallel.ForEach。但是有时我会收到一条错误消息,提示“ ExecuteReader需要打开且可用的连接。连接的当前状态为连接中”。据我了解,由于12个查询中的许多查询都使用相同的InitialCatalog,因此12个查询中并没有真正的新连接,这可能是问题所在吗?我怎样才能解决这个问题?“ sql

  • 我正在尝试使用、和(不使用)实现实时相机应用程序 所以,我发现这篇教程 http://altitudelabs.com/blog/real-time-filter/ 它是用Objective-C编写的,所以我在Swift4.0中重写了那个代码,xcode9 它看起来工作很好,但有时(很少),它崩溃了以下错误。调用的方法时 EXC_BAD_ACCESS(代码=1,地址+0x************)

  • 问题内容: 示例代码: 在代码中,我已经执行了一些无效的读取和无效的写入,但是这个小程序可以正常工作,并且不会创建。 但是一旦进入我的大库,每当我进行1个字节的无效读取或无效写入时,它总是在创建核心转储。 题: 为什么有时我会通过无效的读/写操作获得核心转储,而有时却没有获得核心转储? 问题答案: 您想要做的基本上是缓冲区溢出&在您的代码示例中,更具体地说是堆溢出。您有时只看到崩溃的原因取决于您正

  • 我想用我的相机拍摄1920x1080视频,但我遇到了两个问题 初始化视频捕获时,它会将宽度/高度更改为640/480 当我试图在cv2中更改宽度/高度时,图像变得混乱 当在cv2中设置1920x1080时,图像变成蓝色,底部有一个故障条 根据v4l2-ctl,事情是这样的。蓝色图像似乎不是像素格式变化的结果(例如。RGB至BGR) 最后,这里是一个以640x480拍摄的具有正确颜色的图像示例。代码

  • 问题内容: 看起来我偶然发现了一个元类地狱,即使我不想做任何事情。 我正在使用PySide在Qt4中编写一个应用程序。我想将事件驱动的部分与UI定义分开,UI定义是从Qt Designer文件生成的。因此,我创建了一个“ controller”类,但是为了简化我的生活,我还是对它们进行了多重继承。一个例子: 这按预期工作。它也有继承自(,,)。但是,当我子类化并尝试从所述子类继承(代替)时,会收到

  • 我正在使用新的ShapeableImageView制作一个圆形图像,但覆盖层显示为黑色。我怎样才能使它透明?这是它的样子: 这是代码: