您的位置:首页 > 移动开发 > IOS开发

ios ffmpeg加字幕

2016-03-17 13:09 429 查看
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

rawdownloadcloneembedreportprintC++
2.63 KB

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;

}

}

RAW Paste Data
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: