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函数的使用有一点的示例,下边是文章里的代码,先拷贝下来,以防删掉就找不到了
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;
}
}
这篇文章里讲到了怎么用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;
}
}
相关文章推荐
- ffmpeg ios代码加字幕相关
- iOS ffmpeg视频合并加字幕
- iOS中几种定时器
- iOS开发实用工具——CornerStone配置SVN
- IOS使用正则表达式去掉html中的标签元素,获得纯文本
- iOS 阿拉伯数字转化为中文数字
- 分享一个仿ios风格的AlertDialog的实现
- iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager(三)
- iOS/OS X内存管理借助工具解决内存问题
- iOS学习之iOS沙盒(sandbox)机制和文件操作(二)
- 苹果开发者账号注册申请流程
- iOS学习之iOS沙盒(sandbox)机制和文件操作(一)
- iOS 数组去重总结
- iOS 离屏渲染的研究
- IOS动态为类添加属性
- 40个国人iOS技术博客
- iOS基础框架
- ios 启动程序时隐藏状态栏,启动后显示状态栏
- ios js互调
- 手动加支付宝遇到的错误--iOS