ios ffmpeg加字幕

http://www.yaosansi.com/post/ffmpeg-burn-subtitles-into-video/

这篇文章里讲到了怎么用ffmpeg命令实现加字幕以及烧字幕


http://srtlibrary.weebly.com/

这个网址不知道是不是srt字幕格式的官方网站


http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105484275f03d03860c0d0622e89cc55c20311300667e2e833674d4485ca28335eed1e09fdf047256b527de1ccdf893acabce63f2ef86673205add074c84&p=9833d35b86cc41af5dabe62d021486&newp=89759a45d6c618e50cfbc7710f4493231601d13523808c0a3e83fe4898645e180d32f5&user=baidu&fm=sc&query=ffmpeg+encode_subtitle&qid=e8ed885e00060a97&p1=5

这篇代码里对avcodec_encode_subtitle函数的使用有一点的示例,下边是文章里的代码,先拷贝下来,以防删掉就找不到了

ffmpeg decode/encode subtitle

a guest Sep 23rd, 2013 191 Never
  1. void saveSubtitle ( AVFormatContext *context, Stream stream )
  2. {
  3.   stringstream outfile ;
  4.   outfile << "/tmp/subtitle_" << stream. index << ".srt" ;
  5.   string filename = outfile. str ( ) ;
  6.   AVStream *avstream = context - >streams [stream. index ] ;
  7.   AVCodec *codec = avcodec_find_decoder ( avstream - >codec - >codec_id ) ;
  8.   int result = avcodec_open2 ( avstream - >codec, codec, NULL ) ;
  9.   checkResult ( result == 0, "Error opening codec" ) ;
  10.   cerr << "found codec: " << codec << ", open result= " << result << endl ;
  11.   AVOutputFormat *outFormat = av_guess_format ( NULL, filename. c_str ( ), NULL ) ;
  12.   checkResult ( outFormat ! = NULL, "Error finding format" ) ;
  13.   cerr << "Found output format: " << outFormat - >name << " (" << outFormat - >long_name << ")" << endl ;
  14.  
  15.   AVFormatContext *outFormatContext ;
  16.   avformat_alloc_output_context2 ( &outFormatContext, NULL, NULL, filename. c_str ( ) ) ;
  17.   AVCodec *encoder = avcodec_find_encoder ( outFormat - >subtitle_codec ) ;
  18.   checkResult ( encoder ! = NULL, "Error finding encoder" ) ;
  19.   cerr << "Found encoder: " << encoder - >name << endl ;
  20.  
  21.  
  22.   AVStream *outStream = avformat_new_stream ( outFormatContext, encoder ) ;
  23.   checkResult ( outStream ! = NULL, "Error allocating out stream" ) ;
  24.   AVCodecContext *c = outStream - >codec ;
  25.  
  26.   result = avcodec_get_context_defaults3 ( c, encoder ) ;
  27.   checkResult ( result == 0, "error on get context default" ) ;
  28.  
  29.  
  30.  
  31.   cerr << "outstream codec: " << outStream - >codec << endl ;
  32.   cerr << "Opened stream " << outStream - >id << ", codec=" << outStream - >codec - >codec_id << endl ;
  33.   result = avio_open ( &outFormatContext - >pb, filename. c_str ( ), AVIO_FLAG_WRITE ) ;
  34.   checkResult ( result == 0, "Error opening out file" ) ;
  35.   cerr << "out file opened correctly" << endl ;
  36.   result = avformat_write_header ( outFormatContext, NULL ) ;
  37.   checkResult (result == 0, "Error writing header" ) ;
  38.   cerr << "header wrote correctly" << endl ;
  39.   result = 0 ;
  40.   AVPacket pkt ;
  41.   av_init_packet ( &pkt ) ;
  42.   pkt. data = NULL ;
  43.   pkt. size = 0 ;
  44.  
  45.   cerr << "srt codec id: " << AV_CODEC_ID_SUBRIP << endl ;
  46.   while ( av_read_frame ( context, &pkt ) >= 0 )
  47.   {
  48.     if (pkt. stream_index ! = stream. index )
  49.       continue ;
  50.     int gotSubtitle = 0 ;
  51.     AVSubtitle subtitle ;
  52.     result = avcodec_decode_subtitle2 ( avstream - >codec, &subtitle, &gotSubtitle, &pkt ) ;
  53.     uint64_t bufferSize = 1024 * 1024 ;
  54.     uint8_t *buffer = new uint8_t [bufferSize ] ;
  55.     memset (buffer, 0, bufferSize ) ;
  56.     if ( result >= 0 )
  57.     {
  58.       result = avcodec_ encode_subtitle ( outStream - >codec, buffer, bufferSize, &subtitle ) ;
  59.       cerr << "Encode subtitle result: " << result << endl ;
  60.     }
  61.  
  62.     cerr << "Encoded subtitle: " << buffer << endl ;
  63.     delete [ ] buffer ;
  64.   }
  65. }
RAW Paste Data

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值