ios ffmpeg加字幕
阿新 • • 發佈:2019-01-25
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- void saveSubtitle( AVFormatContext*context, Stream stream )
- {
- stringstream outfile;
-
outfile << "/tmp/subtitle_"<< stream.index
<<
- 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
- 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;
- }
- }