"faststart"导致写入无效的 MP4 文件-6ren"> "faststart"导致写入无效的 MP4 文件-我正在为视频设置格式布局,如下所示: AVOutputFormat* outputFormat = ffmpeg.av_guess_format(null, "output.mp4", null); -6ren">
gpt4 book ai didi

c# - FFmpeg "movflags"> "faststart"导致写入无效的 MP4 文件

转载 作者:太空狗 更新时间:2023-10-29 23:43:59 24 4
gpt4 key购买 nike

我正在为视频设置格式布局,如下所示:

AVOutputFormat* outputFormat = ffmpeg.av_guess_format(null, "output.mp4", null);

AVCodec* videoCodec = ffmpeg.avcodec_find_encoder(outputFormat->video_codec);

AVFormatContext* formatContext = ffmpeg.avformat_alloc_context();
formatContext->oformat = outputFormat;
formatContext->video_codec_id = videoCodec->id;

ffmpeg.avformat_new_stream(formatContext, videoCodec);

这就是我设置编解码器上下文的方式:

AVCodecContext* codecContext = ffmpeg.avcodec_alloc_context3(videoCodec);
codecContext->bit_rate = 400000;
codecContext->width = 1280;
codecContext->height = 720;
codecContext->gop_size = 12;
codecContext->max_b_frames = 1;
codecContext->pix_fmt = videoCodec->pix_fmts[0];
codecContext->codec_id = videoCodec->id;
codecContext->codec_type = videoCodec->type;
codecContext->time_base = new AVRational
{
num = 1,
den = 30
};

我正在使用以下代码为视频标题设置“movflags”>“faststart”选项:

AVDictionary* options = null;

int result = ffmpeg.av_dict_set(&options, "movflags", "faststart", 0);

int writeHeaderResult = ffmpeg.avformat_write_header(formatContext, &options);

文件打开,文件头内容如下:

if ((formatContext->oformat->flags & ffmpeg.AVFMT_NOFILE) == 0)
{
int ioOptionResult = ffmpeg.avio_open(&formatContext->pb, "output.mp4", ffmpeg.AVIO_FLAG_WRITE);
}

int writeHeaderResult = ffmpeg.avformat_write_header(formatContext, &options);

在此之后,我将每个视频帧写成如下:

outputFrame->pts = frameIndex;

packet.flags |= ffmpeg.AV_PKT_FLAG_KEY;
packet.pts = frameIndex;
packet.dts = frameIndex;

int encodedFrame = 0;
int encodeVideoResult = ffmpeg.avcodec_encode_video2(codecContext, &packet, outputFrame, &encodedFrame);

if (encodedFrame != 0)
{
packet.pts = ffmpeg.av_rescale_q(packet.pts, codecContext->time_base, m_videoStream->time_base);
packet.dts = ffmpeg.av_rescale_q(packet.dts, codecContext->time_base, m_videoStream->time_base);
packet.stream_index = m_videoStream->index;

if (codecContext->coded_frame->key_frame > 0)
{
packet.flags |= ffmpeg.AV_PKT_FLAG_KEY;
}

int writeFrameResult = ffmpeg.av_interleaved_write_frame(formatContext, &packet);
}

之后,我写预告片:

int writeTrailerResult = ffmpeg.av_write_trailer(formatContext);

文件完成写入,所有内容都正确关闭并释放。但是,MP4 文件无法播放(甚至 VLC 也无法播放)。 AtomicParsley.exe 也不会显示有关该文件的任何信息。

用于 AutoGen 库的 DLL 是:

avcodec-56.dll
avdevice-56.dll
avfilter-5.dll
avformat-56.dll
avutil-54.dll
postproc-53.dll
swresample-1.dll
swscale-3.dll

最佳答案

似乎单独使用“faststart”会导致问题。只要您通过“movflags”(在我的例子中是“frag_duration”)正确设置您的分段大小,就应该没问题。

此外,最好确保视频中的两个流都正确同步。参见 this有关此问题的答案。

关于c# - FFmpeg "movflags"> "faststart"导致写入无效的 MP4 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38070319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com