android - 从原生 c 代码 android 创建文件

标签 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);
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;
   } 

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

最佳答案

videoOut = fopen("file:/mnt/sdcard/videoout.mp4","wb");
fopen() call 采用文件名,而不是 URI。删除 file:部分。 (除非你真的想打开一个相对路径,它的第一个组件是一个名为“file:”的目录。)

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

关于android - 从原生 c 代码 android 创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19422444/

相关文章:

android - Retrofit/Rxjava 和基于 session 的服务

image - AVFrame 到 RGB - 解码伪影

Android NDK 套接字 connect() 在 3g 上失败时返回 0

python - 避免 PowerShell 中的 Schtask 重复

ffmpeg - 如何在centos 7上安装libx265以用于ffmpeg构建

android - (Android)如何显示图像的一部分?

Java/Android,是否可以获取对象的内容?

android - 加快 Android 编辑调试周期

android - 如何解决 "ERROR: libx264 not found"?

android - 如何在 Android 上获取 Vorbis 库?