ffmpeg 字幕解码
原文:
https://blog.csdn.net/u011283226/article/details/102241233
【写在前面】
在前一篇,我已经讲过了读取外挂字幕并显示的方法:理解过滤图并使用字幕过滤器
但是,全字幕不仅仅是外挂字幕,还有内封字幕和内嵌字幕,因此我们还得考虑其他两种字幕。
不过,对于内嵌字幕,我们根本不需要解码,因为它是直接绘制在视频图像上的。
所以,本篇只需要讲解内封字幕的解码方法,主要内容有:
1、ass 等格式内封字幕解码。
2、sub+idx 格式内封字幕解码。
3、同步视频和字幕。
【正文开始】
- 首先是内封字幕:
我们知道,所谓内封字幕,就是将字幕文件(可能是srt, ass)封装在视频容器中,成为字幕流。
因此只要确定视频存在字幕流( ass等 ),就可以使用和外挂字幕一样的方法进行解码。
当然了,略微有些不同,先来看看代码:
- AVFormatContext *formatContext = nullptr;
- AVCodecContext *videoCodecContext = nullptr, *subCodecContext = nullptr;
- AVStream *videoStream = nullptr, *subStream = nullptr;
- int videoIndex = -1, subIndex = -1;
- //打开输入文件,并分配格式上下文
- avformat_open_input(&formatContext, m_filename.toStdString().c_str(), nullptr, nullptr);
- avformat_find_stream_info(formatContext, nullptr);
- //找到视频流,字幕流的索引
- for (size_t i = 0; i < formatContext->nb_streams; ++i) {
- if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
- videoIndex = int(i);
- videoStream = formatContext->streams[i];
- } else if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
- subIndex = int(i);
- subStream = formatContext->streams[i];
- }
- }
- //打印相关信息,在 stderr
- av_dump_format(formatContext, 0, "format", 0);
- fflush(stderr);
- if (!open_codec_context(videoCodecContext, videoStream)) {
- qDebug() << "Open Video Context Failed!";
- return;
- }
- if (!open_codec_context(subCodecContext, subStream)) {
- //字幕流打开失败,也可能是没有,但无影响,接着处理
- qDebug() << "Open Subtitle Context Failed!";
- }
这块代码就是简单的找到视频流和字幕流,并打开相关上下文( Context ),如果不懂,可以前往第一篇 视频解码。
然后我们继续往下看:
- m_fps = videoStream->avg_frame_rate.num / videoStream->avg_frame_rate.den;
- m_width = videoCodecContext->width;
- m_height = videoCodecContext->height;
- //初始化filter相关
- AVRational time_base = videoStream->time_base;
- QString args = QString::asprintf("video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
- m_width, m_height, videoCodecContext->pix_fmt, time_base.num, time_base.den,
- videoCodecContext->sample_aspect_ratio.num, videoCodecContext->sample_aspect_ratio.den);
- qDebug() << "Video Args: " << args;
- AVFilterContext *buffersrcContext = nullptr;
- AVFilterContext *buffersinkContext = nullptr;
- bool subtitleOpened = false;
- //如果有字幕流
- if (subCodecContext) {
- //字幕流直接用视频名即可
- QString subtitleFilename = m_filename;
- subtitleFilename.replace('/', "\\\\");
- subtitleFilename.insert(subtitleFilename.indexOf(":\\"), char('\\'));
- QString filterDesc = QString("subtitles=filename='%1':original_size=%2x%3")
- .arg(subtitleFilename).arg(m_width).arg(m_height);
- qDebug() << "Filter Description:" << filterDesc.toStdString().c_str();
- subtitleOpened = init_subtitle_filter(buffersrcContext, buffersinkContext, args, filterDesc);
- if (!subtitleOpened) {
- qDebug() << "字幕打开失败!";
- }
- } else {
- //没有字幕流时,在同目录下寻找字幕文件
- //字幕相关,使用subtitles,目前测试的是ass,但srt, ssa, ass, lrc都行,改后缀名即可
- int suffixLength = QFileInfo(m_filename).suffix().length();
- QString subtitleFilename = m_filename.mid(0, m_filename.length() - suffixLength - 1) + ".ass";
- if (QFile::exists(subtitleFilename)) {
- //初始化subtitle filter
- //绝对路径必须转成D\:\\xxx\\test.ass这种形式, 记住,是[D\:\\]这种形式
- //toNativeSeparator()无用,因为只是 / -> \ 的转换
- subtitleFilename.replace('/', "\\\\");
- subtitleFilename.insert(subtitleFilename.indexOf(":\\"), char('\\'));
- QString filterDesc = QString("subtitles=filename='%1':original_size=%2x%3")
- .arg(subtitleFilename).arg(m_width).arg(m_height);
- qDebug() << "Filter Description:" << filterDesc.toStdString().c_str();
- subtitleOpened = init_subtitle_filter(buffersrcContext, buffersinkContext, args, filterDesc);
- if (!subtitleOpened) {
- qDebug() << "字幕打开失败!";
- }
- }
- }
1、如果存在字幕流( if (subCodecContext) ),那么就初始化一个字幕过滤器,字幕过滤器的参数是:

要注意,对于外挂字幕而言,filename 即为字幕文件名,而对于内封字幕,fliename 为视频文件名,格式为:[ D\:\\ ]。
2、如果不存在存在字幕流,那么就寻找同目录下的外挂字幕。
- 然而,这只是 ass 等格式的内封字幕,对于 sub+idx 格式的内嵌字幕,就需要我们自己解码、绘制了。
我们知道,sub+idx 是图形字幕格式,sub 包含了一系列的字幕位图,idx 则是其索引。
当然,对于内部如何我们无需知晓,因为 ffmpeg 会将其解码,具体如下:
- SubtitleFrame subFrame;
- //读取下一帧
- while (m_runnable && av_read_frame(formatContext, packet) >= 0) {
- if (packet->stream_index == videoIndex) {
- //发送给解码器
- int ret = avcodec_send_packet(videoCodecContext, packet);
- while (ret >= 0) {
- //从解码器接收解码后的帧
- ret = avcodec_receive_frame(videoCodecContext, frame);
- frame->pts = frame->best_effort_timestamp;
- if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break;
- else if (ret < 0) goto Run_End;
- //如果字幕成功打开,则输出使用subtitle filter过滤后的图像
- if (subtitleOpened) {
- if (av_buffersrc_add_frame_flags(buffersrcContext, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 0)
- break;
- while (true) {
- ret = av_buffersink_get_frame(buffersinkContext, filter_frame);
- if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break;
- else if (ret < 0) goto Run_End;
- QImage videoImage = convert_image(filter_frame);
- m_frameQueue.enqueue(videoImage);
- av_frame_unref(filter_frame);
- }
- } else {
- //未打开字幕过滤器或无字幕
- if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break;
- else if (ret < 0) goto Run_End;
- QImage videoImage = convert_image(frame);
- //如果需要显示字幕,就将字幕覆盖上去
- if (frame->pts >= subFrame.pts && frame->pts <= (subFrame.pts + subFrame.duration)) {
- videoImage = overlay_subtitle(videoImage, subFrame.image);
- }
- m_frameQueue.enqueue(videoImage);
- }
- av_frame_unref(frame);
- }
- } else if (packet->stream_index == subIndex) {
- AVSubtitle subtitle;
- int got_frame;
- int ret = avcodec_decode_subtitle2(subCodecContext, &subtitle, &got_frame, packet);
- if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break;
- else if (ret < 0) goto Run_End;
- if (got_frame > 0) {
- //如果是图像字幕,即sub + idx
- //实际上,只需要处理这种即可
- if (subtitle.format == 0) {
- for (size_t i = 0; i < subtitle.num_rects; i++) {
- AVSubtitleRect *sub_rect = subtitle.rects[i];
- int dst_linesize[4];
- uint8_t *dst_data[4];
- //注意,这里是RGBA格式,需要Alpha
- av_image_alloc(dst_data, dst_linesize, sub_rect->w, sub_rect->h, AV_PIX_FMT_RGBA, 1);
- SwsContext *swsContext = sws_getContext(sub_rect->w, sub_rect->h, AV_PIX_FMT_PAL8,
- sub_rect->w, sub_rect->h, AV_PIX_FMT_RGBA,
- SWS_BILINEAR, nullptr, nullptr, nullptr);
- sws_scale(swsContext, sub_rect->data, sub_rect->linesize, 0, sub_rect->h, dst_data, dst_linesize);
- sws_freeContext(swsContext);
- //这里也使用RGBA
- QImage image = QImage(dst_data[0], sub_rect->w, sub_rect->h, QImage::Format_RGBA8888).copy();
- av_freep(&dst_data[0]);
- //subFrame存储当前的字幕
- //只有图像字幕才有start_display_time和start_display_time
- subFrame.pts = packet->pts;
- subFrame.duration = subtitle.end_display_time - subtitle.start_display_time;
- subFrame.image = image;
- }
- } else {
- //如果是文本格式字幕:srt, ssa, ass, lrc
- //可以直接输出文本,实际上已经添加到过滤器中
- qreal pts = packet->pts * av_q2d(subStream->time_base);
- qreal duration = packet->duration * av_q2d(subStream->time_base);
- const char *text = const_int8_ptr(packet->data);
- qDebug() << "[PTS: " << pts << "]" << endl
- << "[Duration: " << duration << "]" << endl
- << "[Text: " << text << "]" << endl;
- }
- }
- }
- 先来看 else if (packet->stream_index == subIndex) 部分:
1、使用 avcodec_decode_subtitle2() 获取一帧字幕。
2、subtilte.format 存储字幕格式,为0代表图像字幕。
3、subtitle.rects 存储了字幕位图,因此我们只需要将其转换成想要的图像格式,然后覆盖( overlay )在视频图像上即可。
4、这里需要小小的注意一下,因为视频和字幕并不是同时解码的,并且,字幕会持续一段时间,也就是说,可能很多帧视频使用同一帧字幕,所以我们要同步视频和字幕,这里使用了一个 SubtitleFrame,它的定义如下:
- struct SubtitleFrame {
- QImage image;
- int64_t pts;
- int64_t duration;
- };
我的同步方法是:videoFrame.pts >= subFrame.pts && videoFrame.pts <= subFrame.pts + subFrame.duration,即 视频帧的显示时间戳处于[字幕开始, 字幕结束]之间时,就显示字幕。
现在我们回到 if (subtitleOpened) 这里。
1、如果字幕已经成功打开( ass等格式的外挂字幕或内封字幕 ),我们就直接使用字幕过滤器将字幕添加到视频帧。
2、如果字幕未能成功打开( 为sub+idx格式或没有字幕 ),我们就将 subFrame 覆盖到视频帧上,注意,subFrame 我们在 else if (packet->stream_index == subIndex) 中已经得到了,当然,如果没有则其为空。
其中,conver_image() 和 overlay_subtitle() 很简单,所以直接看源码就好了。
至此,内封字幕讲解完毕。
ffmpeg 字幕解码的更多相关文章
- ffmpeg编解码视频导致噪声增大的一种解决方法
一.前言 ffmpeg在视音频编解码领域算是一个比较成熟的解决方案了.公司的一款视频编辑软件正是基于ffmpeg做了二次封装,并在此基础上进行音视频的编解码处理.然而,在观察编码后的视频质量时,发现图 ...
- FFmpeg编解码处理4-音频编码
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10584948.html FFmpeg编解码处理系列笔记: [0]. FFmpeg时间戳详 ...
- FFmpeg编解码处理3-视频编码
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10584937.html FFmpeg编解码处理系列笔记: [0]. FFmpeg时间戳详 ...
- FFmpeg编解码处理2-编解码API详解
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10584925.html FFmpeg编解码处理系列笔记: [0]. FFmpeg时间戳详 ...
- FFmpeg编解码处理1-转码全流程简介
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10584901.html FFmpeg编解码处理系列笔记: [0]. FFmpeg时间戳详 ...
- 最近在研究FFmpeg编解码
好几年没上CNBLOGS了, 最近在研究FFmpeg编解码,一个人研究感到很寂寞,所以想通过博客来和大家分享和交流,呵呵. 最近研究的主题是: ANDROID手机同屏技术: 需要用到ANDROID截屏 ...
- ffmpeg 编解码详细过程
ffmpeg编解码详细过程 bobbypollo 转:ffmpeg编解码详细过程 原文地址:ffmpeg编解码详细过程(转)作者:心在飞翔原文出处: http://www.360doc.com ...
- 视频编解码---x264用于编码,ffmpeg用于解码
项目要用到视频编解码,最近半个月都在搞,说实话真是走了很多弯路,浪费了很多时间.将自己的最终成果记录于此,期望会给其他人提供些许帮助. 参考教程: http://ffmpeg.org/trac/ffm ...
- 学习FFmpeg API – 解码视频
本文转载 视频播放过程 首先简单介绍以下视频文件的相关知识.我们平时看到的视频文件有许多格式,比如 avi, mkv, rmvb, mov, mp4等等,这些被称为容器(Container), 不同的 ...
- 【转】学习FFmpeg API – 解码视频
ffmpeg是编解码的利器,用了很久,以前看过dranger 的教程,非常精彩,受益颇多,是学习ffmpeg api很好的材料.可惜的是其针对的ffmpeg版本已经比较老了,而ffmpeg的更新又很快 ...
随机推荐
- Bert-vits2最终版Bert-vits2-2.3云端训练和推理(Colab免费GPU算力平台)
对于深度学习初学者来说,JupyterNoteBook的脚本运行形式显然更加友好,依托Python语言的跨平台特性,JupyterNoteBook既可以在本地线下环境运行,也可以在线上服务器上运行.G ...
- ElasticSearch之Exists API
检查指定名称的索引是否存在. 命令样例如下: curl -I "https://localhost:9200/testindex_002?pretty" --cacert $ES_ ...
- Pikachu漏洞靶场 XXE(xml外部实体注入漏洞)
XXE(xml外部实体注入漏洞) 概述 XXE -"xml external entity injection" 既"xml外部实体注入漏洞". 概括一下就是& ...
- ClickHouse的JOIN算法选择逻辑以及auto选项
ClickHouse的JOIN算法选择逻辑以及auto选项 ClickHouse中的JOIN的算法有6种: Direct; Partial merge; Hash; Grace hash; Full ...
- 标注工具合集(点云&图片)
有什么问题欢迎留言交流,发现好用的会持续更新-- 图片类 1. labelimg:https://github.com/tzutalin/labelImg --- 只能拉框 2. labelme:ht ...
- (转)Harbor 启用镜像扫描功能方法
A demo environment with the latest Harbor stable build installed. For additional information please ...
- 全国网络安全行业职业技能大赛云南省选拔赛 Misc 部分WP
word_sercet 题目 我的解答: 加密文档,010打开图片发现密码 VVV_123.com 解压打开得到flag(注:flag原本是隐藏的,但我之前设置过隐藏文字自动显示,因此这里直接可以看到 ...
- root无法下载
在 Linux 中,root 用户已经拥有系统中的最高权限.然而,如果你需要确保 root 用户具有所有权限,可以通过以下步骤来进行:(这是不对的) sudo chown -R root:root / ...
- C# 将Excel转为OFD、UOS
本文以C#及VB.NET代码为例展示如何将Excel工作簿转换为OFD和UOS格式.通过workbook.LoadFromFile(string fileName)方法加载Excel源文档后,然后调用 ...
- 对象存储只能按文件名搜索,你out了吧
摘要:不少大公司的一个桶里都是几亿几十亿的对象,那他们都是怎么检索的呢? 本文分享自华为云社区<对象存储只能按文件名搜索? 用 DWR + ElasticSearch 实现文件名.文件内容.图片 ...