Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void saveSubtitle( AVFormatContext *context, Stream stream )
- {
- stringstream outfile;
- outfile << "/tmp/subtitle_" << stream.index << ".srt";
- string filename = outfile.str();
- AVStream *avstream = context->streams[stream.index];
- AVCodec *codec = avcodec_find_decoder( avstream->codec->codec_id );
- int result = avcodec_open2( avstream->codec, codec, NULL );
- checkResult( result == 0, "Error opening codec" );
- cerr << "found codec: " << codec << ", open result= " << result << endl;
- AVOutputFormat *outFormat = av_guess_format( NULL, filename.c_str(), NULL );
- checkResult( outFormat != NULL, "Error finding format" );
- cerr << "Found output format: " << outFormat->name << " (" << outFormat->long_name << ")" << endl;
- AVFormatContext *outFormatContext;
- avformat_alloc_output_context2( &outFormatContext, NULL, NULL, filename.c_str() );
- AVCodec *encoder = avcodec_find_encoder( outFormat->subtitle_codec );
- checkResult( encoder != NULL, "Error finding encoder" );
- cerr << "Found encoder: " << encoder->name << endl;
- AVStream *outStream = avformat_new_stream( outFormatContext, encoder );
- checkResult( outStream != NULL, "Error allocating out stream" );
- AVCodecContext *c = outStream->codec;
- result = avcodec_get_context_defaults3( c, encoder );
- checkResult( result == 0, "error on get context default" );
- cerr << "outstream codec: " << outStream->codec << endl;
- cerr << "Opened stream " << outStream->id << ", codec=" << outStream->codec->codec_id << endl;
- result = avio_open( &outFormatContext->pb, filename.c_str(), AVIO_FLAG_WRITE );
- checkResult( result == 0, "Error opening out file" );
- cerr << "out file opened correctly" << endl;
- result = avformat_write_header( outFormatContext, NULL );
- checkResult(result==0, "Error writing header");
- cerr << "header wrote correctly" << endl;
- result = 0;
- AVPacket pkt;
- av_init_packet( &pkt );
- pkt.data = NULL;
- pkt.size = 0;
- cerr << "srt codec id: " << AV_CODEC_ID_SUBRIP << endl;
- while( av_read_frame( context, &pkt ) >= 0 )
- {
- if(pkt.stream_index != stream.index)
- continue;
- int gotSubtitle = 0;
- AVSubtitle subtitle;
- result = avcodec_decode_subtitle2( avstream->codec, &subtitle, &gotSubtitle, &pkt );
- uint64_t bufferSize = 1024 * 1024 ;
- uint8_t *buffer = new uint8_t[bufferSize];
- memset(buffer, 0, bufferSize);
- if( result >= 0 )
- {
- result = avcodec_encode_subtitle( outStream->codec, buffer, bufferSize, &subtitle );
- cerr << "Encode subtitle result: " << result << endl;
- }
- cerr << "Encoded subtitle: " << buffer << endl;
- delete [] buffer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement