1. 程式人生 > >ios ffmpeg加字幕

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 guestSep 23rd, 2013 191 Never C++ 2.63 KB
  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.   AVFormatContext *outFormatContext;
  15.   avformat_alloc_output_context2( &outFormatContext, NULL, NULL, filename.c_str());
  16.   AVCodec *encoder = avcodec_find_encoder( outFormat->subtitle_codec);
  17.   checkResult( encoder != NULL, "Error finding encoder" );
  18.   cerr << "Found encoder: " << encoder->name<< endl;
  19.   AVStream *outStream = avformat_new_stream( outFormatContext, encoder );
  20.   checkResult( outStream != NULL, "Error allocating out stream" );
  21.   AVCodecContext *c = outStream->codec;
  22.   result = avcodec_get_context_defaults3( c, encoder);
  23.   checkResult( result == 0, "error on get context default");
  24.   cerr << "outstream codec: " << outStream->codec<< endl;
  25.   cerr << "Opened stream " << outStream->id<< ", codec=" << outStream->codec->codec_id<< endl;
  26.   result = avio_open(&outFormatContext->pb, filename.c_str(), AVIO_FLAG_WRITE);
  27.   checkResult( result == 0, "Error opening out file");
  28.   cerr << "out file opened correctly" << endl;
  29.   result = avformat_write_header( outFormatContext,NULL );
  30.   checkResult(result==0,"Error writing header");
  31.   cerr << "header wrote correctly" << endl;
  32.   result = 0;
  33.   AVPacket pkt;
  34.   av_init_packet( &pkt);
  35.   pkt.data = NULL;
  36.   pkt.size = 0;
  37.   cerr << "srt codec id: " << AV_CODEC_ID_SUBRIP << endl;
  38.   while( av_read_frame( context,&pkt ) >=0 )
  39.   {
  40.     if(pkt.stream_index!= stream.index)
  41.       continue;
  42.     int gotSubtitle =0;
  43.     AVSubtitle subtitle;
  44.     result = avcodec_decode_subtitle2( avstream->codec,&subtitle, &gotSubtitle, &pkt );
  45.     uint64_t bufferSize = 1024 * 1024 ;
  46.     uint8_t *buffer= new uint8_t[bufferSize];
  47.     memset(buffer,0, bufferSize);
  48.     if( result>= 0 )
  49.     {
  50.       result = avcodec_encode_subtitle( outStream->codec, buffer, bufferSize, &subtitle );
  51.       cerr << "Encode subtitle result: " << result << endl;
  52.     }
  53.     cerr << "Encoded subtitle: " << buffer << endl;
  54.     delete [] buffer;
  55.   }
  56. }
RAW Paste Data