首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >远程视频到mp4容器

远程视频到mp4容器
EN

Stack Overflow用户
提问于 2020-02-14 13:33:50
回答 1查看 907关注 0票数 0

如何将rawvideo流到mp4容器中?错误是Could not find tag for codec rawvideo in stream #0, codec not currently supported in container。例如,Url是video=Logitech HD Webcam C270,格式是dshow。文件名是让我们说out.mp4

代码语言:javascript
复制
AVFormatContext* pInputFmtCtx = avformat_alloc_context();
AVInputFormat* inputFormat = av_find_input_format(Format);

avformat_open_input(&pInputFmtCtx, url, inputFormat, null);
if (avformat_find_stream_info(pInputFmtCtx, null) != -1)
   ... find stream index

AVCodec* videoDecoder = avcodec_find_decoder(pInputFmtCtx->streams[_vidStreamIndex]->codecpar->codec_id);

AVCodecContext* videcCodecCtx = avcodec_alloc_context3(videoDecoder);
avcodec_parameters_to_context(videcCodecCtx, videoCodecParams);

avcodec_open2(videcCodecCtx, videoDecoder, null);

// and the output context
AVFormatContext* pOutputFmtCtx = null;
avformat_alloc_output_context2(&pOutputFmtCtx, null, null, fileName);

// iterate over streams of input context and when we find it
AVStream* in_stream = pInputFmtCtx->streams[i];
AVCodecParameters* in_codecpar = in_stream->codecpar;

AVStream* out_stream = avformat_new_stream(pOutputFmtCtx, null);

// init h264 encoder
AVCodec* videoEncoder = ffmpeg.avcodec_find_encoder(AVCodecID.AV_CODEC_ID_H264);
pVideoEncoderCodecContext = ffmpeg.avcodec_alloc_context3(videoEncoder);

pVideoEncoderCodecContext->time_base = videcCodecCtx->time_base;
pVideoEncoderCodecContext->framerate = videcCodecCtx->framerate;
pVideoEncoderCodecContext->width = videcCodecCtx->width;
pVideoEncoderCodecContext->height = videcCodecCtx->height;
pVideoEncoderCodecContext->bit_rate = videcCodecCtx->bit_rate;
pVideoEncoderCodecContext->gop_size = videcCodecCtx->gop_size;
pVideoEncoderCodecContext->pix_fmt = AVPixelFormat.AV_PIX_FMT_YUV420P;
pVideoEncoderCodecContext->flags |= ffmpeg.AV_CODEC_FLAG_GLOBAL_HEADER;

// copy parameters to outstream codec
avcodec_parameters_copy(out_stream->codecpar, in_codecpar);

....

// after that
avio_open(&pOutputFmtCtx->pb, fileName, AVIO_FLAG_WRITE);
avformat_write_header(pOutputFmtCtx, &opts);

// and reading
while (av_read_frame(pInputFormatContext, pkt) >= 0)

// decode 
avcodec_send_packet(videcCodecCtx, pkt);
//receive the raw frame from the decoder
avcodec_receive_frame(videcCodecCtx, frame);

// now encode if its video packet
int ret = avcodec_send_frame(pVideoEncoderCodecContext, frame);
if (ret < 0)
{
    continue;
}

while (ret >= 0)
{
    ret = avcodec_receive_packet(pVideoEncoderCodecContext, packet);

    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
    {
           return;
    }

    av_packet_rescale_ts(packet, pVideoEncoderCodecContext->time_base, pOutputFmtCtx->streams[packet->stream_index]->time_base);
    av_interleaved_write_frame(pOutputFmtCtx, pkt);

    av_packet_unref(packet);
}

这很好,如果相机流H264,但如果相机流的and视频,它不会流到文件,并抛出错误。

编辑,就像我现在试图对它进行编码一样,但是avcodec_send_frame()返回-22,没有任何东西保存到文件中。我错过哪里了?编辑的代码。

EN

回答 1

Stack Overflow用户

发布于 2020-02-14 15:52:04

Mp4不支持Mp4视频。您必须将其编码为另一种格式,或者使用支持它的容器。

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

https://stackoverflow.com/questions/60227384

复制
相关文章

相似问题

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