Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a native fd int to FFMPEG from openable URI

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?

like image 995
Steve M Avatar asked May 06 '17 01:05

Steve M


People also ask

How to select the output format of each frame with ffmpeg?

You can select the output format of each frame with ffmpeg by specifying the audio and video codec and format. For example to compute the CRC of the input audio converted to PCM unsigned 8-bit and the input video converted to MPEG-2 video, use the command:

How do I encode a video file using FFmpeg?

Use ffmpeg to encode the input, and send the output to three different destinations. The dump_extra bitstream filter is used to add extradata information to all the output video keyframes packets, as requested by the MPEG-TS format. The select option is applied to out.aac in order to make it contain only audio packets.

How do I print the CRC value from FFmpeg?

You can print the CRC to stdout with the command: You can select the output format of each frame with ffmpeg by specifying the audio and video codec and format. For example to compute the CRC of the input audio converted to PCM unsigned 8-bit and the input video converted to MPEG-2 video, use the command:

What are the available identifiers in FFmpeg?

Available identifiers are "$RepresentationID$", "$Number$", "$Bandwidth$" and "$Time$". In addition to the standard identifiers, an ffmpeg-specific "$ext$" identifier is also supported.


1 Answers

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;
 } 
like image 87
William Seemann Avatar answered Nov 11 '22 17:11

William Seemann