Android 从本机c代码创建文件
我正在尝试使用FFMPEG库在android应用程序中解复用视频。一切正常。但是,当我试图从c代码创建一个文件以在文件中写入不同的流时,fopen返回NULL。在那之后我不能继续了。我的代码是Android 从本机c代码创建文件,android,android-ndk,ffmpeg,Android,Android Ndk,Ffmpeg,我正在尝试使用FFMPEG库在android应用程序中解复用视频。一切正常。但是,当我试图从c代码创建一个文件以在文件中写入不同的流时,fopen返回NULL。在那之后我不能继续了。我的代码是 int ret , got_frame; av_register_all(); LOGE("Registered formats"); err = av_open_input_file(&pFormatCtx, "file:/mnt/sdcard/input.mp4", NULL, 0, NULL
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;
}
int-ret,得到了帧;
av_寄存器_all();
LOGE(“注册格式”);
err=av_open_input_file(&pFormatCtx,“file:/mnt/sdcard/input.mp4”,NULL,0,NULL);
LOGE(“称为打开文件”);
如果(错误!=0){
LOGE(“无法打开文件”);
返回;
}
LOGE(“打开的文件”);
如果(av_查找_流_信息(pFormatCtx)流[i]->codec->codec_类型==codec_类型_视频){
视频流=i;
如果(音频流!=-1)
打破
}
如果(pFormatCtx->streams[i]->codec->codec\u type==codec\u type\u音频){
音频流=i;
如果(视频流!=-1)
打破
}
}
如果(视频流==-1){
LOGE(“无法找到视频流”);
返回;
}
如果(音频流==-1){
LOGE(“无法找到音频流”);
返回;
}
LOGI(“视频流是[%d]音频流[%d]”,视频流,音频流);
pCodecCtx=pFormatCtx->streams[videoStream]->编解码器;
pCodecCtxAudio=pFormatCtx->streams[audioStream]->编解码器;
LOGI(“视频大小为[%d x%d]”,pCodecCtx->width,pCodecCtx->height);
videoOut=fopen(“文件:/mnt/sdcard/videoOut.mp4”,“wb”);
如果(videoOut==NULL){
LOGE(“无法打开输出视频”);
返回;
}
我的代码有什么问题。我已启用应用程序在外部存储中写入的权限。另外,我指定的路径也是正确的。帮帮我
videoOut = fopen("file:/mnt/sdcard/videoout.mp4","wb");
fopen()
调用采用文件名,而不是URI。删除文件:
零件。(除非您确实想要打开相对路径,其第一个组件是名为“file:”的目录。)
失败后记录
errno
的值也是一个好主意。尽管你真的不应该这样硬编码路径,因为这段代码在各种设备上都会失败,比如Android 4.2+平板电脑上,代码是在一个辅助帐户下运行的。那么我如何从C代码在SD卡中创建一个文件呢。你能给我一些想法或样品吗。