MediaMuxer无法生成可流式传输的MP4文件。

8
我正在使用MediaExtractor在Android上编辑MP4,以获取音频和视频轨道,然后使用MediaMuxer创建一个新文件。这很好用。我可以在手机上(和其他播放器上)播放新的MP4文件,但无法在Web上流式传输文件。当我停止MediaMuxer时,它会生成一个日志消息:“mp4文件无法流式传输。” 我查看了底层本地代码(MPEG4Writer.cpp),似乎写入器在计算所需moov盒子大小时遇到了问题。如果没有将比特率作为参数提供给写入器,则尝试使用一些启发式方法进行猜测。问题是MediaMuxer不提供设置MPEG4Writer参数的能力。我是否漏掉了什么,或者我是否被困在寻找生成文件(或标题)的其他方式中?谢谢。
2个回答

8
在MPEG4Writer.cpp文件中:
// The default MIN_MOOV_BOX_SIZE is set to 0.6% x 1MB / 2,
// where 1MB is the common file size limit for MMS application.
// The default MAX _MOOV_BOX_SIZE value is based on about 3
// minute video recording with a bit rate about 3 Mbps, because
// statistics also show that most of the video captured are going
// to be less than 3 minutes.

这是对MediaMuxer使用方式的错误假设。我们正在录制最多15秒的高分辨率视频,MIN_MOOV_BOX_SIZE太小了。因此,为了使文件可流式传输,我必须重写文件以将moov头移动到mdat之前并修补一些偏移量。以下是我的代码。它不是很好。错误路径没有正确处理,并且它对盒子的顺序做出了假设。

public void fastPlay(String srcFile, String dstFile)  {
    RandomAccessFile inFile = null;
    FileOutputStream outFile = null;
    try {
        inFile = new RandomAccessFile(new File(srcFile), "r");
        outFile = new FileOutputStream(new File(dstFile));
        int moovPos = 0;
        int mdatPos = 0;
        int moovSize = 0;
        int mdatSize = 0;
        byte[] boxSizeBuf = new byte[4];
        byte[] pathBuf = new byte[4];
        int boxSize;
        int dataSize;
        int bytesRead;
        int totalBytesRead = 0;
        int bytesWritten = 0;

        // First find the location and size of the moov and mdat boxes
        while (true) {
            try {
                boxSize = inFile.readInt();
                bytesRead = inFile.read(pathBuf);
                if (bytesRead != 4) {
                    Log.e(TAG, "Unexpected bytes read (path) " + bytesRead);
                    break;
                }
                String pathRead = new String(pathBuf, "UTF-8");
                dataSize = boxSize - 8;
                totalBytesRead += 8;
                if (pathRead.equals("moov")) {
                    moovPos = totalBytesRead - 8;
                    moovSize = boxSize;
                } else if (pathRead.equals("mdat")) {
                    mdatPos = totalBytesRead - 8;
                    mdatSize = boxSize;
                }
                totalBytesRead += inFile.skipBytes(dataSize);
            } catch (IOException e) {
                break;
            }
        }

        // Read the moov box into a buffer. This has to be patched up. Ug.
        inFile.seek(moovPos);
        byte[] moovBoxBuf = new byte[moovSize]; // This shouldn't be too big.
        bytesRead = inFile.read(moovBoxBuf);
        if (bytesRead != moovSize) {
            Log.e(TAG, "Couldn't read full moov box");
        }

        // Now locate the stco boxes (chunk offset box) inside the moov box and patch
        // them up. This ain't purdy.
        int pos = 0;
        while (pos < moovBoxBuf.length - 4) {
            if (moovBoxBuf[pos] == 0x73 && moovBoxBuf[pos + 1] == 0x74 &&
                    moovBoxBuf[pos + 2] == 0x63 && moovBoxBuf[pos + 3] == 0x6f) {
                int stcoPos = pos - 4;
                int stcoSize = byteArrayToInt(moovBoxBuf, stcoPos);
                patchStco(moovBoxBuf, stcoSize, stcoPos, moovSize);
            }
            pos++;
        }

        inFile.seek(0);
        byte[] buf = new byte[(int) mdatPos];
        // Write out everything before mdat
        inFile.read(buf);
        outFile.write(buf);

        // Write moov
        outFile.write(moovBoxBuf, 0, moovSize);

        // Write out mdat
        inFile.seek(mdatPos);
        bytesWritten = 0;
        while (bytesWritten < mdatSize) {
            int bytesRemaining = (int) mdatSize - bytesWritten;
            int bytesToRead = buf.length;
            if (bytesRemaining < bytesToRead) bytesToRead = bytesRemaining;
            bytesRead = inFile.read(buf, 0, bytesToRead);
            if (bytesRead > 0) {
                outFile.write(buf, 0, bytesRead);
                bytesWritten += bytesRead;
            } else {
                break;
            }
        }
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    } finally {
        try {
            if (outFile != null) outFile.close();
            if (inFile != null) inFile.close();
        } catch (IOException e) {}
    }
}

private void patchStco(byte[] buf, int size, int pos, int moovSize) {
    Log.e(TAG, "stco " + pos + " size " + size);
    // We are inserting the moov box before the mdat box so all of
    // offsets in the stco box need to be increased by the size of the moov box. The stco
    // box is variable in length. 4 byte size, 4 byte path, 4 byte version, 4 byte flags
    // followed by a variable number of chunk offsets. So subtract off 16 from size then
    // divide result by 4 to get the number of chunk offsets to patch up.
    int chunkOffsetCount = (size - 16) / 4;
    int chunkPos = pos + 16;
    for (int i = 0; i < chunkOffsetCount; i++) {
        int chunkOffset = byteArrayToInt(buf, chunkPos);
        int newChunkOffset = chunkOffset + moovSize;
        intToByteArray(newChunkOffset, buf, chunkPos);
        chunkPos += 4;
    }
}

public static int byteArrayToInt(byte[] b, int offset)
{
    return   b[offset + 3] & 0xFF |
            (b[offset + 2] & 0xFF) << 8 |
            (b[offset + 1] & 0xFF) << 16 |
            (b[offset] & 0xFF) << 24;
}

public void intToByteArray(int a, byte[] buf, int offset)
{
    buf[offset] = (byte) ((a >> 24) & 0xFF);
    buf[offset + 1] = (byte) ((a >> 16) & 0xFF);
    buf[offset + 2] = (byte) ((a >> 8) & 0xFF);
    buf[offset + 3] = (byte) (a  & 0xFF);
}

0

目前,MediaMuxer無法創建流媒體MP4文件。

您可以在https://software.intel.com/en-us/intel-inde上嘗試使用Intel INDE和Android媒體包(INDE的一部分),https://software.intel.com/en-us/articles/intel-inde-media-pack-for-android-tutorials上提供了相關教程。它有一個示例展示如何使用媒體套件創建並在網絡上流傳文件。

例如,對於相機流傳輸,它具有名為CameraStreamerActivity.java的示例。

public void onCreate(Bundle icicle) {

    capture = new CameraCapture(new AndroidMediaObjectFactory(getApplicationContext()), progressListener);

    parameters = new StreamingParameters();
    parameters.Host = getString(R.string.streaming_server_default_ip);
    parameters.Port = Integer.parseInt(getString(R.string.streaming_server_default_port));
    parameters.ApplicationName = getString(R.string.streaming_server_default_app);
    parameters.StreamName = getString(R.string.streaming_server_default_stream);

    parameters.isToPublishAudio = false;
    parameters.isToPublishVideo = true;
}

public void startStreaming() {
    configureMediaStreamFormat();
    capture.setTargetVideoFormat(videoFormat);
    capture.setTargetAudioFormat(audioFormat);
    capture.setTargetConnection(prepareStreamingParams());
    capture.start();
}

此外,还有类似的示例用于文件流或游戏进程捕获和流式传输。

我并不一定想从服务器流传文件。我希望浏览器能够加载一个H264编码的MP4文件并且平稳地播放它。如果文件中的头信息被正确设置,那么浏览器将能够在读取文件时开始播放它,就像服务器正在流传它一样。无论如何,我相信你是正确的,MediaMuxer不能胜任这项工作。我会看看Intel INDE。感谢你的指引。 - mbert65

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接