androidvideotrimandroid-ffmpeglossless

Lossless video trimming with milliseconds using FFMPEG


I've spent a few weeks going through and testing all the answers on here for lossless cutting using FFMPEG on Android. No matter what I try I always get loss. In the app we're building we need to be millisecond precise.

I've tried so many variations of the command with a lot of them being more than half a second off. This is for both copy and re-encoding. With this i have tried the -ss before and after as well as in both positions but I don't see any difference. These are the two commands that have worked the best be still not close enough:

//copy
"-ss $startTime -t $endTime -i $input -f segment -c copy $output"

//encode
"-i $input -ss $startTime -c:v libx264 -crf 12 -preset ultrafast -t $endTime $output"

Has anyone used a command or even other library that has given more accurate results?


Solution

  • After looking for days the closest I've come to millisecond precise trimming in FFMPEG is with the following command:

    "-ss $startCutTime -i $inputFilePath -ss 0 -c copy -to $endCutTime -avoid_negative_ts make_zero $outputFilePath"
    

    This command is precise to about a 20th of a millisecond with a 30fps clip but this will all depend on the frame rate of your video. I hope this helps others out. Here's an explanation of what each component does:

    /**
         * FFMPEG Cmd Structure
         * -i: Input file
         * -ss: If this is placed before the input file it seeks to this position in the input file. If it's after the input it will split from that exact position.
         * -to: The position of where the command is going to cut to.
         * -c copy: This copies the video and audio codecs as they are for speed.
         * -avoid_negative_ts make_zero: Sets the first timestamp as 0
         * The times are passed in parsed eg 00:00:10.456
         * The output file is the last part of the command
         */