1

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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
AngryCubeDev
  • 155
  • 2
  • 16
  • are you passing in the number of seconds of a more precise timestamp:https://stackoverflow.com/questions/23171937/ffmpeg-video-editing-command-in-milliseconds-timestamp – danfolkes May 09 '19 at 20:06
  • @danfolkes we're passing in the time stamp as hh:mm:ss.sss eg 00:00:10.538 – AngryCubeDev May 10 '19 at 07:30
  • 1
    My guess is that your input file does not provide an accurate index. Try rewrapping your input before cutting, use e.g. mov container and codec copy. – Harry May 10 '19 at 10:44
  • @Harry thank you very much for the idea. I'll give that a go. Does this take much time? – AngryCubeDev May 10 '19 at 13:09
  • rewrapping is the fastest thing one can do in video processing matters. it should go as fast as your harddrive is or your networking connection. – Harry May 10 '19 at 20:57
  • @Harry I tried it this morning but it has the same amount of accuracy as I had with the MP4. Am I missing anything or is it down to how the video is recorded? – AngryCubeDev May 13 '19 at 10:54
  • Hm hard to guess, i'd Play with different Containers like .mxf as well. However, you must be Aware that using -codec copy it can never be Frame accurate. – Harry May 14 '19 at 08:59

1 Answers1

2

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
     */
AngryCubeDev
  • 155
  • 2
  • 16