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的更新又很快 ...
随机推荐
- Plant-Earth-wp
Earth 信息收集 开放了80,443 只能访问到443,试了试msf里面frado的远古rce都不成功.然后注意到有域名解析 添加到hosts里面再访问,当前页面有几串密文,经尝试在message ...
- C++篇:第十四章_编程_知识点大全
C++篇为本人学C++时所做笔记(特别是疑难杂点),全是硬货,虽然看着枯燥但会让你收益颇丰,可用作学习C++的一大利器 十四.编程 (一)概念 系统函数及其库是 C++语言所必须的,预处理命令不是 C ...
- 云原生2.0时代下,DevOps实践如何才能更加高效敏捷?
当前全球的数字化浪潮逐步加深,云计算成为当今信息化发展的重要基础设施,云原生(Cloud Native)在数字化浪潮中的角色逐步提升,成为近几年云计算领域炙手可热的话题. 首先我们来看看一张图,看看云 ...
- 云小课|MRS基础原理之Hue组件介绍
阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说).深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云.更多精彩内容请单击此处. 摘要:Hue是一组WEB ...
- 如何对APP进行安全加固
如何对APP进行安全加固 引言 如今,移动应用市场蓬勃发展,APP数量呈现爆炸性增长.随着5G技术的广泛应用,APP的增长趋势持续增强.然而,由于APP的泛滥,网络攻击者的目标也在逐渐转移,数亿的 ...
- Solon2 开发之IoC,五、Bean 扫描的三种方式
1.启动时扫描 package org.example.demo; public class DemoApp{ public static void main(String[] args){ // / ...
- PPT 求职应聘:如何制作人物介绍
PPT 求职应聘:如何制作人物介绍 单人展示
- Python 数组比较
a = [1, 2, 3, 5, 6, 5, 7, 8] b = [1, 3, 4, 5, 6, 3, 8, 7] print('A => %s' % a) print('B => %s' ...
- 如何安装和使用 Hugging Face Unity API
Hugging Face Unity API 提供了一个简单易用的接口,允许开发者在自己的 Unity 项目中方便地访问和使用 Hugging Face AI 模型,已集成到 Hugging Face ...
- C#开源免费的Blazor图表库
前言 今天分享一款基于ApexCharts.js封装的.C#开源免费的Blazor图表库:Blazor-ApexCharts. 10款值得推荐的Blazor UI组件库 全面的ASP.NET Core ...