2013-10-17 35 views
0

我想解锁使用FFMPEG库的android应用程序中的视频。一切工作正常。但是当我尝试从c代码创建一个文件来写入文件fopen中的不同流返回NULL。那之后我无法继续。我的代码是从原生c代码创建一个文件android

int ret , got_frame; 
av_register_all(); 
LOGE("Registered formats"); 
err = av_open_input_file(&pFormatCtx, "file:/mnt/sdcard/input.mp4", NULL, 0, NULL); 
LOGE("Called open file"); 
if(err!=0) { 
    LOGE("Couldn't open file"); 
    return; 
} 
LOGE("Opened file"); 

if(av_find_stream_info(pFormatCtx)<0) { 
    LOGE("Unable to get stream info"); 
    return; 
} 

videoStream = -1; 
audioStream = -1; 
for (i=0; i<pFormatCtx->nb_streams; i++) { 
    if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) { 
     videoStream = i; 
     if(audioStream != -1) 
     break; 
    } 

    if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO) { 
       audioStream = i; 
       if(videoStream != -1) 
       break; 
      } 

} 
if(videoStream==-1) { 
    LOGE("Unable to find video stream"); 
    return; 
} 
if(audioStream==-1) { 
     LOGE("Unable to find audio stream"); 
     return; 
    } 

LOGI("Video stream is [%d] Audio stream [%d]", videoStream,audioStream); 

pCodecCtx=pFormatCtx->streams[videoStream]->codec; 

pCodecCtxAudio=pFormatCtx->streams[audioStream]->codec; 

LOGI("Video size is [%d x %d]", pCodecCtx->width, pCodecCtx->height); 

videoOut = fopen("file:/mnt/sdcard/videoout.mp4","wb"); 
if (videoOut == NULL) { 
    LOGE("Unable to open output video"); 
    return; 
    } 

我的代码有什么问题。我为应用程序启用了在外部存储中写入的权限。我指定的路径也是正确的。帮我。

+1

后logcat输出。 – Squonk

回答

1
videoOut = fopen("file:/mnt/sdcard/videoout.mp4","wb"); 

fopen()调用一个文件名,而不是一个URI。删除file:部分。 (除非你真的想打开一个相对路径,第一个组件是一个名为“file:”的目录)。

在失败后记录errno的值也是一个好主意。

+1

尽管您不应该像这样硬编码路径,因为此代码在各种设备上都会失败,例如代码在辅助帐户下运行的Android 4.2+平板电脑。 – CommonsWare

+0

那么我怎样才能从C代码在SD卡中创建一个文件。你能给我一些想法或一些样品。 – Azhagiri