从可打开的URI将本机fd int传递给FFMPEG [英] Passing a native fd int to FFMPEG from openable URI

查看:247
本文介绍了从可打开的URI将本机fd int传递给FFMPEG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Storage Access Framework中的CATEGORY_OPENABLE URI打开文件描述符.我首先尝试使用sdcard上的文件,我已经可以使用_data列将其解析为文件路径并打开(我正在尝试避免这样做,而改用文件描述符).

I am trying to open a file descriptor from a CATEGORY_OPENABLE URI from the Storage Access Framework. I am first trying with a file on the sdcard, which I can already resolve to a file path using the _data column and open (I am trying to get away from doing this, and use the file descriptor instead).

我这样获得本地int fd:

I get the the native int fd like this:

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

然后在C ++中,我试图像这样打开它,这个想法取自

Then in C++, I am trying to open it like this, the idea taken from 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中产生未知错误".如果我使用上面链接到FileDescriptor对象的上述方法中的jni方法jniGetFDFromFileDescriptor来获取int fd,则会发生相同的事情.如何在不使用文件路径的情况下正确地使用FFMPEG打开可打开的URI?

This yields an "Unknown error" from avformat_open_input. The same thing happens if I use the jni method jniGetFDFromFileDescriptor from the above linked on a FileDescriptor object to get the int fd instead. How can I open an openable URI with FFMPEG correctly without using the file path?

推荐答案

我的项目( FFmpegMediaMetadataRetriever )完成此操作.参见此要旨

My project (FFmpegMediaMetadataRetriever) accomplishes this. See this gist or the full file here.

注意:确保已在启用管道协议的情况下构建了FFmpeg!

Note: make sure you have built FFmpeg with the pipe protocol enabled!

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

I used avformat_open_input, dup() the file descriptor and made sure to set the offset via:

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

这篇关于从可打开的URI将本机fd int传递给FFMPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆