2014-10-08 78 views
1

我一直试图将字幕从格式SubRip(.srt)转换为MPEG4 Timed Text将它们复合到一个MP4容器,其中已经有音频和视频。在命令行与ffmpeg这样做很简单:错误打开解码器与avcodec

ffmpeg -i subtitles.srt -i video.mp4 -c:v copy -c:a copy -c:s mov_text videoWithSubtitles.mp4 

然而,使用avcodec中,我能打开这两个文件,并从中读取,但是当我尝试打开AVCodecContext为(编码器)编解码器AV_CODEC_ID_MOV_TEXT,收到以下消息:

Error code: -1094995529 
Error occurred: Invalid data found when processing input 

产生错误的代码如下:

AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_MOV_TEXT); 
if (!codec) { 
    NSLog(@"Error finding encoder"); 
    exit(-1); 
} 

AVCodecContext *codecContext = avcodec_alloc_context3(codec); 
if (!codecContext) { 
    NSLog(@"Error allocating context"); 
    exit(-1); 
} 

if (avcodec_is_open(codecContext) == 0) { 
    NSLog(@"output codec context is closed, opening"); 
    ret = avcodec_open2(codecContext, codec, NULL); 
    if (ret < 0) { 
     NSLog(@"Error code: %i",ret); 
     NSLog(@"Error occurred: %s", av_err2str(ret)); 
     NSLog(@"Error opening encoder"); 
     exit(-1); 
    } 
} 

该代码适用于所有解码器(工作在解码模式下的相同编解码器)以及其他编码器,主要是视频或音频,但它也不适用于AV_CODEC_ID_SUBRIPAV_CODEC_ID_SRT(设置编码器)。从ffmpeg的(error.h从avutil)

+0

我已经确定了问题,** AVCodecContext **需要设置字幕标题。我不知道里面应该包含什么,但是从** SRT **的编解码环境中复制它似乎有诀窍 – marsop 2014-10-08 12:36:20

回答

0

错误代码: http://ffmpeg.org/doxygen/trunk/error_8h_source.html

原来你指定的值是:

#define AVERROR_INVALIDDATA  FFERRTAG('I','N','D','A') 

的-1094995529变得-0x41444E49,当你看看那些ACSII中的字母0x41 ='A',0x44 ='D',0x4E ='N,0x49 ='I'。由于宏/等东西是相反的,所以ADNI成为INDA,你可以从#define片断看到,是定义为FFERRTAG('I','N','D','A')的AVERROR_INVALIDDATA。

错误代码的其余部分是在该文件中,我已经在这里下面粘贴他们:

#define AVERROR_BSF_NOT_FOUND  FFERRTAG(0xF8,'B','S','F') ///< Bitstream filter not found 
#define AVERROR_BUG    FFERRTAG('B','U','G','!') ///< Internal bug, also see AVERROR_BUG2 
#define AVERROR_BUFFER_TOO_SMALL FFERRTAG('B','U','F','S') ///< Buffer too small 
#define AVERROR_DECODER_NOT_FOUND FFERRTAG(0xF8,'D','E','C') ///< Decoder not found 
#define AVERROR_DEMUXER_NOT_FOUND FFERRTAG(0xF8,'D','E','M') ///< Demuxer not found 
#define AVERROR_ENCODER_NOT_FOUND FFERRTAG(0xF8,'E','N','C') ///< Encoder not found 
#define AVERROR_EOF    FFERRTAG('E','O','F',' ') ///< End of file 
#define AVERROR_EXIT    FFERRTAG('E','X','I','T') ///< Immediate exit was requested; the called function should not be restarted 
#define AVERROR_EXTERNAL   FFERRTAG('E','X','T',' ') ///< Generic error in an external library 
#define AVERROR_FILTER_NOT_FOUND FFERRTAG(0xF8,'F','I','L') ///< Filter not found 
#define AVERROR_INVALIDDATA  FFERRTAG('I','N','D','A') ///< Invalid data found when processing input 
#define AVERROR_MUXER_NOT_FOUND FFERRTAG(0xF8,'M','U','X') ///< Muxer not found 
#define AVERROR_OPTION_NOT_FOUND FFERRTAG(0xF8,'O','P','T') ///< Option not found 
#define AVERROR_PATCHWELCOME  FFERRTAG('P','A','W','E') ///< Not yet implemented in FFmpeg, patches welcome 
#define AVERROR_PROTOCOL_NOT_FOUND FFERRTAG(0xF8,'P','R','O') ///< Protocol not found 
#define AVERROR_STREAM_NOT_FOUND FFERRTAG(0xF8,'S','T','R') ///< Stream not found 
#define AVERROR_BUG2    FFERRTAG('B','U','G',' ') 
#define AVERROR_UNKNOWN   FFERRTAG('U','N','K','N') ///< Unknown error, typically from an external library 
#define AVERROR_EXPERIMENTAL  (-0x2bb2afa8) ///< Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it. 
#define AVERROR_INPUT_CHANGED  (-0x636e6701) ///< Input changed between calls. Reconfiguration is required. (can be OR-ed with AVERROR_OUTPUT_CHANGED) 
#define AVERROR_OUTPUT_CHANGED  (-0x636e6702) ///< Output changed between calls. Reconfiguration is required. (can be OR-ed with AVERROR_INPUT_CHANGED) 
#define AVERROR_HTTP_BAD_REQUEST FFERRTAG(0xF8,'4','0','0') 
#define AVERROR_HTTP_UNAUTHORIZED FFERRTAG(0xF8,'4','0','1') 
#define AVERROR_HTTP_FORBIDDEN  FFERRTAG(0xF8,'4','0','3') 
#define AVERROR_HTTP_NOT_FOUND  FFERRTAG(0xF8,'4','0','4') 
#define AVERROR_HTTP_OTHER_4XX  FFERRTAG(0xF8,'4','X','X') 
#define AVERROR_HTTP_SERVER_ERROR FFERRTAG(0xF8,'5','X','X') 

的AVFormatContext是棘手的。我最终得到了我的工作,但我确信我会做很多额外的副本和疯狂的东西。

av_register_all(); 
avformat_network_init(); 

//Allocate space and setup the the object to be used for storing 
// all info needed for this connection 
context = avformat_alloc_context(); 

AVDictionary *opts = 0; 
av_dict_set(&opts, "rtsp_transport", "tcp", 0); 

//open rtsp 
if (avformat_open_input(&context, URL, NULL, &opts) != 0){ 
    return 1; //Error 1 
} 

//maximum time in microseconds during which the input should be analyzed 
context->max_analyze_duration = 10000000; 

if (avformat_find_stream_info(context,NULL) < 0){ 
    return 2; //Error 2 
} 

//search video stream 
for(int i=0; i<(int)context->nb_streams; i++){ 
    if(context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) 
     video_stream_index = i; 
} 

oc = avformat_alloc_context(); 

//start reading packets from stream and write them to file 
av_read_play(context); //start the stream 

if(curVidFormat == MJPEG) { 
    codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); 
} 
else { 
    codec = avcodec_find_decoder(AV_CODEC_ID_H264); 
} 

if (!codec) return 3; //Error 3 
ccontext = avcodec_alloc_context3(codec);  

avcodec_get_context_defaults3(ccontext, codec); 
avcodec_copy_context(ccontext,context->streams[video_stream_index]->codec); 

int errorCode = avcodec_open2(ccontext, codec, NULL); 
if(errorCode<0) 
{ 
    printf("avcodec_open2 failed with errorCode:%d !",errorCode); 
    exit(1); 
} 

//create frame to be used for storing frames coming out 
// of avcodec_decode_video2 
int curAVFramesize = avpicture_get_size(PIX_FMT_YUV420P, 
    ccontext->width, ccontext->height); 
curAVFramePicBuffer = (uint8_t*)(av_malloc(curAVFramesize)); 
curAVFrame=avcodec_alloc_frame(); 
avpicture_fill((AVPicture *)curAVFrame,curAVFramePicBuffer, 
    PIX_FMT_YUV420P,ccontext->width, ccontext->height); 
returnAVFramesize = avpicture_get_size(PIX_FMT_YUV420P, ccontext->width, ccontext->height); 

//allocate and create frame to be used for resizing of frames 
int resizedFramesize = avpicture_get_size(PIX_FMT_YUV420P, outputWidth, 
    outputHeight); 
resizedFramePicBuffer = (uint8_t*)(av_malloc(resizedFramesize)); 
resizedFrame=avcodec_alloc_frame(); 
avpicture_fill((AVPicture *)resizedFrame,resizedFramePicBuffer, 
    PIX_FMT_YUV420P,outputWidth, outputHeight); 

//Could be used to convert between formats 
//In this case just scaling the image 
img_convert_ctx = sws_getContext(ccontext->width, ccontext->height, 
    ccontext->pix_fmt, width, height, PIX_FMT_YUV420P, 
    SWS_BICUBIC, NULL, NULL, NULL); 

很显然,我没有做字幕在这里,但如果你要加载字幕把它们写在如定时文本,这会让你应该将它们装入格式方面的意义。