【问题标题】:Extract (demux) subtitles using avcodec_decode_subtitle2 (FFmpeg)使用 avcodec_decode_subtitle2 (FFmpeg) 提取(解复用)字幕
【发布时间】:2018-05-20 08:46:17
【问题描述】:

我找到了一些代码,对其进行了一些编辑以打印字幕文本,但我不确定如何将字幕保存到文件中(从视频文件中提取,例如 mkv)

接下来的代码只是打印了很多行,并不是所有的都包含字幕文本

std::ofstream out ("/path/to/extracted/subtitles.srt");

while(av_read_frame(pFormatCtx, &pkt) == 0) {
    int got_frame = 0;
    int ret = avcodec_decode_subtitle2(aCodecCtx, subtitle, &got_frame, &pkt);
    if (ret >= 0 && got_frame) {
        AVSubtitleRect **rects = subtitle->rects;
        for (i = 0; i < subtitle->num_rects; i++) {
            AVSubtitleRect rect = *rects[i];
            if (rect.type == SUBTITLE_ASS) {
                printf("ASS %s", rect.ass);
            } else if (rect.x == SUBTITLE_TEXT) {;
                printf("TEXT %s", rect.text);
            }
        }
        // it just writes some big file (similar to videofile size)
        //out.write((char*)pkt.data, pkt.size);
    }
}
out.close();
//... 

【问题讨论】:

    标签: c++ ffmpeg ifstream subtitle


    【解决方案1】:

    您正在对照 SUBTITLE_TEXT 检查“rect.x”,此时应该是“rect.type”,如上面的条件所示。

    【讨论】:

      猜你喜欢
      • 2016-05-18
      • 2015-12-31
      • 2012-05-30
      • 2016-06-21
      • 1970-01-01
      • 2012-08-21
      • 2014-01-07
      • 1970-01-01
      • 2013-07-29
      相关资源
      最近更新 更多