应用坠毁avformat_find_stream_info

问题描述:

我写了一个本地方法简单地得到一个视频的编解码器的ID,但我的应用程序从来没有过这条线“avformat_find_stream_info(pFormatCtx,NULL)” 这里是我的本地方法:应用坠毁avformat_find_stream_info

int get_video_info(JNIEnv *env, jobject thiz, jstring strInFile){ 
AVFormatContext *pFmtCtx; 
AVCodecContext *pCCtx; 
AVCodec *pCd; 
AVFrame *picture; 
int i, videoStream = -1, error = 0; 

const char *in_file_name = (*env)->GetStringUTFChars(env, strInFile, NULL); 

av_register_all(); 

LOGI(1, "HERE 0 %s", in_file_name); // app passed here 

/*Open video file*/ 
pFmtCtx = avformat_alloc_context(); 
if((error=avformat_open_input(&pFmtCtx, in_file_name, NULL, NULL)) < 0){ 
    LOGE(1, "Couldn't open file, error-code: %d with file url: %s", error, in_file_name); 
    return -1; 
} 

LOGI(1, "HERE 1 Duration: %d", pFmtCtx->duration); //app passed here 

/*Retrieve the stream information, APP CRASH RIGHT HERE*/ 
if(avformat_find_stream_info(pFormatCtx, NULL)<0){ 
    LOGE(1, "Couldn't retrieve stream information"); 
    avformat_free_context(pFmtCtx); 
    return -1; // Couldn’t find stream information 
} 

LOGI(1, "HERE 2"); 

//Find the first video stream 
videoStream=-1; 
for(i=0; i<pFormatCtx->nb_streams; i++) { 
    if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){ 
     videoStream=i; 
     break; 
    } 
} 

if(videoStream==-1){ 
    avformat_free_context(pFmtCtx); 
    LOGE(1, "Didn't find a video stream"); 
    return -1; // Didn’t find a video stream 
} 

// Get a pointer to the codec context for the video stream 
pCCtx=pFormatCtx->streams[videoStream]->codec; 

avformat_free_context(pFmtCtx); 
(*env)->ReleaseStringUTFChars(env, strInFile, in_file_name); 
return pCCtx->codec_id; 
} 

问:为什么我总是找不到像这样的流信息?请帮我修复它。谢谢

您正在代码中的几个地方使用pFormatCtx(未初始化)而不是pFmtCtx!

您可以在avformat_alloc_context()之后设置pFormatCtx = pFmtCtx