首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式使用ffmpeg读取fMP4

以编程方式使用ffmpeg读取fMP4
EN

Stack Overflow用户
提问于 2019-04-07 13:12:02
回答 1查看 670关注 0票数 2

我所有解析fmp4的尝试都失败了,出现了以下错误:

avcodec_send_packet-10949955291返回并打印以下错误:

代码语言:javascript
复制
[h264 @ 0x105001600] No start code is found.
[h264 @ 0x105001600] Error splitting the input into NAL units.

我做了什么?

下载了一个完全可播放的h.264文件,并使用以下cmd将其碎片化

代码语言:javascript
复制
ffmpeg -i long.mp4 -an -sn -vcodec libx264 -force_key_frames "expr:gte(t,n_forced*4)" -sc_threshold 0 -r 25 -f hls -hls_time 4 -hls_list_size 99999 -start_number 1 -hls_segment_type fmp4 -hls_fmp4_init_filename init.mp4 -t 30 -threads 0 big_bunny.m3u8

并使用以下AVIO读取示例

代码语言:javascript
复制
#include <fstream>
#include <iterator>
#include <algorithm>
#include <libavcodec/avcodec.h>

std::vector<unsigned char> b1;
std::vector<unsigned char> b2;
bool initialized = false;
int bytesread = 0;

static int read_packet(void* opaque, uint8_t* buf, int buf_size)
{
    if (b1.size() && !initialized) {
        size_t bytesToRead = std::min(buf_size, (int)b1.size());
        ::memcpy(buf, b1.data(), bytesToRead);
        bytesread += bytesToRead;
        if (bytesread >= b1.size()) {
            initialized = true;
            bytesread = 0;
        }
        return bytesToRead;
    }

    ::memcpy(buf, b2.data() + bytesread, buf_size);
    bytesread += buf_size;
    return buf_size;
}

int main(int argc, char** argv)
{
    AVFormatContext* fmt_ctx = NULL;
    AVIOContext* avio_ctx = NULL;
    uint8_t *buffer = NULL, *buffer2 = nullptr, *avio_ctx_buffer = NULL;
    size_t buffer_size, buffer_size2, avio_ctx_buffer_size = 4096;
    char* input_filename = NULL;
    int ret = 0;
    struct buffer_data bd = {0};

    std::ifstream input1("/Users/x/Downloads/fmp4/init.mp4", std::ios::binary);
    std::ifstream input2("/Users/x/Downloads/fmp4/big_bunny1.m4s", std::ios::binary);

    b1 = std::vector<unsigned char>((std::istreambuf_iterator<char>(input1)), std::istreambuf_iterator<char>());
    b2 = std::vector<unsigned char>((std::istreambuf_iterator<char>(input2)), std::istreambuf_iterator<char>());

    avio_ctx_buffer = (uint8_t*)av_malloc(avio_ctx_buffer_size);

    fmt_ctx = avformat_alloc_context();
    avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, nullptr, &read_packet, NULL, NULL);
    fmt_ctx->pb = avio_ctx;

    AVDictionary* opts = NULL;
    //   av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
    ret = avformat_open_input(&fmt_ctx, NULL, NULL, &opts);
    ret = avformat_find_stream_info(fmt_ctx, NULL);

    AVCodec* decoder = nullptr;

    decoder = avcodec_find_decoder(fmt_ctx->streams[0]->codecpar->codec_id);
    AVCodecContext* decoderCtx = avcodec_alloc_context3(decoder);

    ret = avcodec_open2(decoderCtx, decoder, nullptr);

    AVPacket pkt;
    av_init_packet(&pkt);

    AVFrame* frame = av_frame_alloc();
    while (true) {
        ret = av_read_frame(fmt_ctx, &pkt);

        ret = avcodec_send_packet(decoderCtx, &pkt);
        if (ret != 0)
            assert(false);

        for (;;) {
            ret = avcodec_receive_frame(decoderCtx, frame);
            if (ret < 0) {
                break;
            }

            int g;
            g = 0;
        }
    }

我甚至不确定这是否是处理fmp4类型的正确方法。但是为了清楚起见,我只是将init文件加载到第一个缓冲区中,将实际的媒体文件加载到第二个缓冲区中,然后在缓冲区之间切换到buf_size的值。

EN

回答 1

Stack Overflow用户

发布于 2019-04-08 18:16:04

我不认为在使用avio_alloc_context demuxer的情况下需要的所有信息--参见打开媒体文件,但这是一个疯狂的猜测,因为您的输出没有显示来自mp4 demuxer的错误,而只显示来自H.264比特流解析器的错误,所以我倾向于探测的文件格式可以是原始H.264,但很难分辨。但是,如果我弄错了,您没有提供一个查找回调,而拆分一个mp4当然需要寻求跳转框,然后寻找样本数据。所以,我首先尝试提供一个查找回调,看看是否会被调用。但是,从示例中还不能确定为什么需要I/O回调,因为数据驻留在一个文件中,而不是在内存位置,所以我建议尝试使用标准的输入方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55559373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档