Mobile-ffmpeg: video compression is very slow

Created on 6 Oct 2020  路  6Comments  路  Source: tanersener/mobile-ffmpeg

Description
i have integrated this library 4.4.LTS in my android app. trying to compress video along with trimming i felt it is very slow.how to improve the speed of compression
i have used this command

arrayOf("-y", "-i", source.absolutePath, "-ss",
            startTime, "-to", endTime, "-s", "$newWidth*$newHeight", "-b", "$newBitRate", outPutFile.absolutePath) 

Expected behavior
it could be more fast

Current behavior
video compression is very slow

All 6 comments

Seeking should be before the input:

arrayOf("-y", "-ss", startTime, "-to", endTime, "-i", source.absolutePath, "-s", "$newWidth*$newHeight", "-b", "$newBitRate", outPutFile.absolutePath)

This should increase the speed.


FFmpeg related questions should be asked on stackoverflow instead.

Thank you @HBiSoft
yes, it improved the speed little bit (initial part of compression). but still it is slow while doing compression. can you please advise me to improve the compression speed.

currently i am using this command

arrayOf( "-y", "-ss", startTime, "-to", endTime, "-i", source.absolutePath, "-s", "$newWidth*$newHeight", "-b", "$newBitRate", "-strict", "experimental", "-preset", "ultrafast", "-crf", "28", "-vcodec", "mpeg4", outPutFile.absolutePath )

In the first part of my answer here:

There is always a trade-off when choosing between speed and quality.


You said "but still it is slow while doing compression", but how slow is slow?
I mean, how long does it take to encode a 10MB video, for example?


Other things that might cause longer waiting time is

  • "$newWidth*$newHeight" the larger the video, the longer it will take
  • $newBitRate the higher the bitrate, the longer it will take

Also, I'm not sure why you are using "-strict", "experimental", it's not necessary. You also don't have to specify the codec, you can simply add .mp4 after your output name, like this outPutFile.absolutePath+".mp4".

Thank you @HBiSoft

i am not good in ffmpeg commands,

here i am reducing the video size and bitrate

when { width >= 1920 || height >= 1920 -> { newWidth = (width * 0.5).toInt() newHeight = (height * 0.5).toInt() newBitRate = (bitrate * 0.5).roundToInt() } width >= 1280 || height >= 1280 -> { newWidth = (width * 0.75).toInt() newHeight = (height * 0.75).toInt() newBitRate = (bitrate * 0.75).roundToInt() } width >= 720 || height >= 720 -> { newWidth = width newHeight = height newBitRate = bitrate } else -> { newWidth = (width * 0.9).toInt() newHeight = (height * 0.9).toInt() newBitRate = (bitrate * 0.9).roundToInt() } }

basically i try to compress a video along with trimming (60sec max).
i have tried with a video of 167mb and it takes 3.40mins.(same video i have check with tiktok it takes only 1min)

how could i get compression speed same as tiktok?
is that possible do any kind of hardware acceleration with ffmpeg?

please correct me if i am wrong.

@shaheercs
Apps like TikTok and WhatsApp are trimming the video at the closest I-Frame(Keyframe) from the given timestamp. In other words, not trimming the video at the exact position as what the user entered/provided. They are most probably using a combination of MediaCodec, MediaExtractor and MediaMuxer to handle video trimming (and probably playback as well).

If you are looking at implementing the same behavior as TikTok, have a look at Transcoder, it makes use of the above mentioned API's. I tested it by trimming 60 seconds from a video (186MB) and it took 46 seconds.


On the other hand, using FFmpeg you could also copy the video/audio streams and it will reduce the waiting time drastically. You can copy the video/audio stream using -c:v copy and -c:a copy. Your command would then look like this:

arrayOf("-ss", startTime, "-i", source.absolutePath, "-to", endTime, "-c:v", "copy", "-c:a", "copy", "-crf", "28", "-preset", "ultrafast", outPutFile.absolutePath )

This will decrease your waiting time drastically, but keep in mind that it will now trim the video at the closest keyframe from the given timestamp.


The thing with FFmpeg is, you have to test different commands to find the one that works for your use case.

I hope this helped.


Please close the issue and if you have any other FFmpeg related questions, you can open an issue on Super User and use the ffmpeg tag.

Thank you for your great support.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chaocharleswang picture chaocharleswang  路  4Comments

brainstormapps picture brainstormapps  路  7Comments

ZedZeal picture ZedZeal  路  6Comments

fayte0618 picture fayte0618  路  3Comments

arabsiphone picture arabsiphone  路  4Comments