2

I have ported ffmpeg library to Android. Using JNI interface, I am able to run ffmpeg commands by giving arguments to ffmpeg's main method, just like from command line.

In order to get a specific part of a video, I use this command :

ffmpeg -i /mnt/sdcard/input_video.mp4 -ss 00:00:12 -t 00:00:10 -an /mnt/sdcard/output_video.mp4

and it works great. The video is split from 12. seconds to 22. seconds and the video is saved, the method returns normally (as 0).

However, if I make a second similar call (different start time for example) just after the first one is completed, ffmpeg is not able to process the request and it throws a segmentation fault.

For the first call, it gives such an info :

Guessed Channel Layout for Input Stream #0.0 : mono

and works. But for the second, the message is like this one:

Guessed Channel Layout for Input Stream #1.0 : mono

and it doesn't work. I don't know if it has something to do with the error.

The problem, in general, should be related to static global variables (I think) but I could not manage to reset them properly. What might be the solution to make multiple successful calls to the main method of ffmpeg?

  • How are you able to call ffmpeg main() from your source? I can call other ffmpeg libraries (e.g., libavcodec, etc.) methods but cannot call ffmpeg main. I'm trying to do the same thing as you are doing - copying a part of a video to another file to be able to split it. – vida Sep 25 '13 at 14:09

2 Answers2

1

https://github.com/jhotovy/android-ffmpeg

Calling ffmpeg’s main() more than once from the same Activity causes segfaults. This is still an issue with libffmpeg, but libffmpeginvoke at least applies the band-aid solution described here: Calling native method twice of third party library in an Activity causes the Android application to close down.

Community
  • 1
  • 1
Swilly
  • 11
  • 1
1

This example from github made by Hiko can help others fixing the problem. JNI invoke ffmpeg more than once

what it does is it re-initialize static variables in the begining of main() methode like this:

int main(int argc, char **argv)
{
    LOGI("start run in main.");
    received_sigterm = 0;
    received_nb_signals = 0;
    transcode_init_done = 0;
    ffmpeg_exited = 0;
    main_return_code = 0;

    run_as_daemon  = 0;
    nb_frames_dup = 0;
    nb_frames_drop = 0;

    nb_input_streams = 0;
    nb_input_files   = 0;
    nb_output_streams = 0;
    nb_output_files   = 0;
    nb_filtergraphs = 0;
    int ret;
    int64_t ti;

    register_exit(ffmpeg_cleanup);
    ............................................
    ............................................
    ........... The rest of the code ...........

By adding those lines you will never get the segfault again.

SaidTagnit
  • 115
  • 3
  • 14