将本机 fd int 从可打开的 URI 传递到 FFMPEG

Passing a native fd int to FFMPEG from openable URI

我正在尝试从存储访问框架的 CATEGORY_OPENABLE URI 打开文件描述符。我首先尝试使用 sdcard 上的文件,我已经可以使用 _data 列将其解析为文件路径并打开(我试图避免这样做,而是使用文件描述符)。

我得到了这样的原生 int fd:

int fd = getContentResolver().openFileDescriptor(data.getData(), "r").detachFd();

然后在 C++ 中,我试图像这样打开它,这个想法取自 How to properly pass an asset FileDescriptor to FFmpeg using JNI in Android:

pFormatCtx = avformat_alloc_context();
pFormatCtx->iformat = av_find_input_format("mp3");

char path[50];
sprintf(path, "pipe:%d", fd);

int e;
if(e=(avformat_open_input(&pFormatCtx,path,NULL,NULL)!=0)){
    av_strerror(e, path, 50);
    return error;
}

这会从 avformat_open_input 生成 "Unknown error"。如果我使用上面链接到 FileDescriptor 对象的 jni 方法 jniGetFDFromFileDescriptor 来获取 int fd,也会发生同样的事情。如何在不使用文件路径的情况下使用 FFMPEG 正确打开可打开的 URI?

@Steve M,您可能从这些帖子中得到了答案:例如: 1. ffmpeg encoding sample wanted?

您终于探索了这个项目。 https://github.com/illuusio/ffmpeg-example

祝你好运! @Stev M

也许这是一个愚蠢的观察,但您是否尝试替换:

avformat_open_input(&avFormatPtr, "dummyFilename", nullptr, nullptr);

(avformat_open_input(&pFormatCtx,path,NULL,NULL)

替换为:

(avformat_open_input(&pFormatCtx,path,nullptr,nullptr)

?

我的项目(FFmpegMediaMetadataRetriever) accomplishes this. See this gist or the full file 这里。

注意: 确保你构建的 FFmpeg 启用了管道协议!

我使用了 avformat_open_inputdup() 文件描述符并确保通过以下方式设置偏移量:

 if (state->offset > 0) {
        state->pFormatCtx = avformat_alloc_context();
        state->pFormatCtx->skip_initial_bytes = state->offset;
 }