我编写了一个本机方法来简单地获取视频的编解码器ID,但我的应用程序从未传递此行'avformat_find_stream_info(pFormatCtx,NULL)' 这是我的原生方法:
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;
}
问题:为什么我总是找不到像这样的流信息?请帮我修理一下。感谢
答案 0 :(得分:1)
您在代码中的多个位置使用pFormatCtx(未初始化)而不是pFmtCtx!
您可以在avformat_alloc_context()之后设置pFormatCtx = pFmtCtx
: