Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
                }
like image 663
priojeet priyom Avatar asked Mar 15 '26 16:03

priojeet priyom


1 Answers

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.

like image 108
Alex Cohn Avatar answered Mar 17 '26 08:03

Alex Cohn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!