赞
踩
从代码层面以及自由度来说,用ffmpeg来写全能播放组件是最佳方案(跨平台最好最多、编解码能力最强),尽管已经有优秀的vlc/mpv等方案可以直接用,但是vlc/mpv对标主要是播放器应用层面,其他层面比如视频监控行业领域就比较鸡肋,所以还是从底层一点一滴做解码编码会让自己更熟练。关于网上很多ffmpeg的示例,尤其是播放的示例,数不胜数,比较适合用来入门学习,问题是随着ffmpeg官方不断的迭代更新,很多代码都不可用,因为api变了,尤其是最近5年迭代的特别快,从2017年开始直接猛飚版本,现在直接干到了ffmpeg6版本,一般在安排取消或者改动某些api接口前几个版本,都会打上对应的标记,既有新方法,也兼容旧的api,一般会放在下一个大版本将旧的api接口移除,以便减轻历史包袱,在核心功能编解码这块,一直是兼容的,不会说新版本不兼容以前旧版本的一些编解码格式。
编写这个全能播放组件,面对用户各种各样的需求,当然需要从ffmpeg2兼容到ffmpeg6以及后续的版本,现在用的最多的还是ffmpeg4版本,目测三五年后会陆续切换到ffmpeg5/ffmpeg6,主要是支持的格式多了,尤其是某些新标准的编解码的效率更高。在ffmpeg提供的头文件接口中,并没有提供ffmpeg的大版本号,只提供了字符串版本,所以需要通过子库的主版本号来定义一个ffmpeg的版本号,比如编解码库LIBAVCODEC_VERSION_MAJOR,56=ffmpeg2/57=ffmpeg3/58=ffmpeg4/59=ffmpeg5/60=ffmpeg6,这个编解码库就是ffmpeg的核心,看家的本领都在里面,个人觉得ffmpeg最牛逼的就是编解码和滤镜。在兼容各个版本的这条路上,大致整理了以下几条:
公众号:Qt实战,各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发。
公众号:Qt入门和进阶,专门介绍Qt/C++相关知识点学习,帮助Qt开发者更好的深入学习Qt。多位Qt元婴期大神,一步步带你从入门到进阶,走上财务自由之路。
//通过avcode版本定义对应主版本 #if (LIBAVCODEC_VERSION_MAJOR == 56) #define FFMPEG_VERSION_MAJOR 2 #elif (LIBAVCODEC_VERSION_MAJOR == 57) #define FFMPEG_VERSION_MAJOR 3 #elif (LIBAVCODEC_VERSION_MAJOR == 58) #define FFMPEG_VERSION_MAJOR 4 #elif (LIBAVCODEC_VERSION_MAJOR == 59) #define FFMPEG_VERSION_MAJOR 5 #elif (LIBAVCODEC_VERSION_MAJOR == 60) #define FFMPEG_VERSION_MAJOR 6 #endif int FFmpegHelper::getRotate(AVStream *stream) { int rotate = 0; //测试发现ffmpeg2不支持旋转滤镜 #if (FFMPEG_VERSION_MAJOR < 3) return rotate; #endif #if (FFMPEG_VERSION_MAJOR < 5) AVDictionaryEntry *tag = NULL; tag = av_dict_get(stream->metadata, "rotate", NULL, 0); if (tag) { rotate = atoi(tag->value); } #else //从ffplay源码中找到的方法 double theta = 0; quint8 *displaymatrix = av_stream_get_side_data(stream, AV_PKT_DATA_DISPLAYMATRIX, NULL); if (displaymatrix) { theta = -av_display_rotation_get((qint32 *) displaymatrix); theta -= 360 * floor(theta / 360 + 0.9 / 360); rotate = theta; } #endif return rotate; } void FFmpegHelper::setRotate(AVStream *stream, int rotate) { #if (FFMPEG_VERSION_MAJOR < 5) av_dict_set(&stream->metadata, "rotate", QString::number(rotate).toUtf8().constData(), 0); #else quint8 *sidedata = av_stream_new_side_data(stream, AV_PKT_DATA_DISPLAYMATRIX, sizeof(qint32) * 9); if (sidedata) { av_display_rotation_set((qint32 *)sidedata, rotate); } #endif } void FFmpegHelper::getStreamInfo(AVStream *stream, int &id, int &width, int &height, qint64 &bitrate, int &sampleRate, int &channelCount, int &profile) { #if (FFMPEG_VERSION_MAJOR < 3) id = stream->codec->codec_id; width = stream->codec->width; height = stream->codec->height; bitrate = stream->codec->bit_rate; sampleRate = stream->codec->sample_rate; channelCount = stream->codec->channels; profile = stream->codec->profile; #else id = stream->codecpar->codec_id; width = stream->codecpar->width; height = stream->codecpar->height; bitrate = stream->codecpar->bit_rate; sampleRate = stream->codecpar->sample_rate; channelCount = stream->codecpar->channels; profile = stream->codecpar->profile; #endif } int FFmpegHelper::copyContext(AVStream *streamIn, AVStream *streamOut) { int result = -1; //设置 codec_tag = 0 这个很关键(不加保存的数据可能不正确) #if (FFMPEG_VERSION_MAJOR < 3) result = avcodec_copy_context(streamOut->codec, streamIn->codec); streamOut->codec->codec_tag = 0; #else result = avcodec_parameters_copy(streamOut->codecpar, streamIn->codecpar); streamOut->codecpar->codec_tag = 0; #endif return result; } int FFmpegHelper::copyContext(AVCodecContext *avctx, AVStream *stream, bool from) { int result = -1; #if (FFMPEG_VERSION_MAJOR < 3) if (from) { result = avcodec_copy_context(stream->codec, avctx); } else { result = avcodec_copy_context(avctx, stream->codec); } #else if (from) { result = avcodec_parameters_from_context(stream->codecpar, avctx); } else { result = avcodec_parameters_to_context(avctx, stream->codecpar); } #endif return result; } int FFmpegHelper::decode(FFmpegThread *thread, AVCodecContext *avctx, AVPacket *packet, AVFrame *frame, bool video) { int result = -1; #ifdef videoffmpeg QString flag = video ? "视频解码" : "音频解码"; #if (FFMPEG_VERSION_MAJOR < 3) if (video) { avcodec_decode_video2(avctx, frame, &result, packet); if (result < 0) { thread->debug(result, flag, "avcodec_decode_video2"); return result; } } else { avcodec_decode_audio4(avctx, frame, &result, packet); if (result < 0) { thread->debug(result, flag, "avcodec_decode_audio4"); return result; } } goto end; #else result = avcodec_send_packet(avctx, packet); if (result < 0 && (result != AVERROR(EAGAIN)) && (result != AVERROR_EOF)) { //if (result < 0) { thread->debug(result, flag, "avcodec_send_packet"); return result; } while (result >= 0) { result = avcodec_receive_frame(avctx, frame); if (result == AVERROR(EAGAIN) || result == AVERROR_EOF) { break; } else if (result < 0) { thread->debug(result, flag, "avcodec_receive_frame"); break; } goto end; } #endif return result; end: //调用线程处理解码后的数据 if (video) { thread->decodeVideo2(packet); } else { thread->decodeAudio2(packet); } #endif return result; } int FFmpegHelper::encode(FFmpegSave *thread, AVCodecContext *avctx, AVPacket *packet, AVFrame *frame, bool video) { int result = -1; #ifdef videosave QString flag = video ? "视频编码" : "音频编码"; #if (FFMPEG_VERSION_MAJOR < 3) if (video) { avcodec_encode_video2(avctx, packet, frame, &result); if (result < 0) { thread->debug(result, flag, "avcodec_encode_video2"); return result; } } else { avcodec_encode_audio2(avctx, packet, frame, &result); if (result < 0) { thread->debug(result, flag, "avcodec_encode_audio2"); return result; } } goto end; #else result = avcodec_send_frame(avctx, frame); if (result < 0) { thread->debug(result, flag, "avcodec_send_frame"); return result; } while (result >= 0) { result = avcodec_receive_packet(avctx, packet); if (result == AVERROR(EAGAIN) || result == AVERROR_EOF) { break; } else if (result < 0) { thread->debug(result, flag, "avcodec_receive_packet"); break; } goto end; } #endif return result; end: thread->writePacket2(packet, video); #endif return result; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。