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

1,如何使用ffmpeg获得视频文件中有多少条音频管道?
2,如何使用ffmpeg获取视频文件中的某条管道的音频文件?
3,如何在指定视频中的每个时间点截图?

皈依舞
浏览 255回答 1
1回答

冉冉说

// 初始化libavcodec, and register all codecs and formatsav_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 播放位置
打开App,查看更多内容
随时随地看视频慕课网APP