为了账号安全,请及时绑定邮箱和手机立即绑定

如何在指定视频中的每个时间点截图?

如何在指定视频中的每个时间点截图?

皈依舞 2022-07-01 07:07:03
1,如何使用ffmpeg获得视频文件中有多少条音频管道?2,如何使用ffmpeg获取视频文件中的某条管道的音频文件?3,如何在指定视频中的每个时间点截图?
查看完整描述

1 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

// 初始化libavcodec, and register all codecs and formats
av_register_all();

// 打开媒体文件
input_format_context = 0;
ret = av_open_input_file(
&input_format_context,
open_file_name.data(),
0,
0,
0);
if(0 != ret || !input_format_context)
{
LOG( "FFMPEG Load File " << open_file_name << " Failed ." << ret);
return ret;
}
else
{
LOG( "FFMPEG Load File " << open_file_name << " Success .");
}

// 取出流信息
ret = av_find_stream_info(input_format_context);
if(0 != ret)
{
LOG( "FFMPEG Can Not Find Stream Info .");
return ret;
}

// 寻找第一个视频流
for(unsigned int i = 0; i < input_format_context->nb_streams; i++)
{
if(input_format_context->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO)
{
video_stream_index = i;
}
else if(input_format_context->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO)
{
audio_stream_index = i;
}
}

if(video_stream_index == -1 && audio_stream_index == -1)
{
// 既没有音频也没有视频,直接退出
LOG("Do not have video stream and audio stream");
return -1;
}

// 得到视频流编码上下文的指针
if(video_stream_index != -1)
{
video_decode_context = input_format_context->streams[video_stream_index]->codec;
// 寻找视频流的解码器
video_decode = avcodec_find_decoder(video_decode_context->codec_id);
if(!video_decode)
{
LOG( "FFMPEG Can Not Decoder For " << (int)video_decode_context->codec_id);
}
// 打开解码器
ret = avcodec_open(video_decode_context,video_decode);
if(0 != ret)
{
LOG( "FFMPEG Can Not Open Video Decoder .");
}

LOG("Image Size " << video_decode_context->width << "*" << video_decode_context->height);
LOG("Frame Rate " << (float)video_decode_context->time_base.den / (float)video_decode_context->time_base.num);
}

// 得到音频流编码上下文的指针
if(audio_stream_index != -1)
{
audio_decode_context = input_format_context->streams[audio_stream_index]->codec;
audio_decode = avcodec_find_decoder(audio_decode_context->codec_id);
if(!audio_decode)
{
LOG( "FFMPEG Can Not Decoder For " << (int)audio_decode_context->codec_id);
}
ret = avcodec_open(audio_decode_context,audio_decode);
if(0 != ret)
{
LOG( "FFMPEG Can Not Open Audio Decoder .");
}
}

读图像或者音频数据

// 从流中读取数据
int frameFinished = 0;
AVPacket packet;
AVFrame* frame = 0;
while(av_read_frame(input_format_context, &packet) >= 0)
{
if(packet.stream_index == video_stream_index)
{
if(frame == 0)
frame = avcodec_alloc_frame();

// 视频
avcodec_decode_video(video_decode_context, frame, &frameFinished,packet.data,packet.size);

第三个 问题 也很简单 会了前2个。搜索ffmpeg 播放位置


查看完整回答
反对 回复 2022-07-05
  • 1 回答
  • 0 关注
  • 200 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号