Passing a native fd int to FFMPEG from openable UR

2020-08-22 06:19发布

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).

I get the the native int fd like this:

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

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;
}

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?

3条回答
迷人小祖宗
2楼-- · 2020-08-22 06:55

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

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

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;
 } 
查看更多
再贱就再见
3楼-- · 2020-08-22 06:56

@Steve M, may be you got your answer from these posts: such as : 1. ffmpeg encoding sample wanted?

and finally you explore this project . https://github.com/illuusio/ffmpeg-example

Best of luck! @Stev M

查看更多
放我归山
4楼-- · 2020-08-22 07:15

Maybe it is a silly observation, but did you try to replace:

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

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

Replace for:

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

?

查看更多
登录 后发表回答