Advertisement
Guest User

ffmpeg decode/encode subtitle

a guest
Sep 23rd, 2013
2,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement