priojeet priyom
priojeet priyom

Reputation: 908

how to multi thread with ffmpeg?

I am trying to split, convert and merge an audio. I used writingMinds FFmpeg build for Android. But it is taking to much time for long duration audios.

To speed it up I tried using the "thread -4" command on a phone having 8 cores but it didn't improve the performance.

So then I split the audio into 4 parts (to use 4 threads) and then called FFmpeg.execute() inside separate threads for multithreading. But FFmpeg library processes the files sequentially. Is it possible for ffmpeg to parallelly process these 4 parts in 4 threads? If so how?

UPDATE
see alex cohn's answer. In this case, it was also because of the calling method of FFmpegExecuteAsyncTask as execute method of Asynctask class only allows one instance to run at a time in modern APIs. a workaround was using this checks.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // Android 3.0 to
                    // Android 4.3
                    // Parallel AsyncTasks are not possible unless using executeOnExecutor
                    ffmpegExecuteAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                } else { // Below Android 3.0
                    // Parallel AsyncTasks are possible, with fixed thread-pool size
                    ffmpegExecuteAsyncTask.execute();
                }

Upvotes: 3

Views: 2864

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57203

You are right. This Java wrapper makes sure all calls to ffmpeg are sequential. If you drop this harness and load the binary yourself, you can run multiple ffmpeg processes in parallel. Note that they are not threads, but full-blown processes, so the cost of doing this may be higher than you expect.

Upvotes: 2

Related Questions