I have a problem with video editing and unsynchronized sound.
What I'd like to achieve:
a) I have a TV tuner, with recording ability. So I have a long .mts file.
b) The file consists of both movie, and commercials. I'd like to cut all the commercials out, and get a movie in .mp4 file (h264, or h265 - it does not matter that much).
--- Part 1 ---
What I do right now:
- I get the .mts file, and check it in VLC player. I do it to find the time moments, when commercials start and stop.
- I cut the .mts file into pieces, using the command:
ffmpeg -ss <START_MOMENT> -i <FILE> -t <DURATION> -c copy <OUTFILE>.mts > /dev/null 2>&1
In this way, I usually end up with three parts of the video I'd like to achieve.
- Having the separated parts (all in the .mts format), I concatenate them:
ffmpeg -f concat -safe 0 -i toconcat.txt -c copy <DEST_FILE>.mts
- Then, as the final step, I encode (and, thus, shrink) the file:
ffmpeg -i <CONCATENATED_FILE>.mts -c:v libx264 -preset fast -crf 25 -c:a aac -b:a 128k <TARGET_VIDEO>.mp4
The problem is: in the final .mp4 file, audio is out of sync. After each "junction", the audio gets (more or less) ahead of video. Sometimes the problem is barely noticable, and sometimes it is very annoying.
--- Part 2 ---
I have also thought about some alternative way to solve the problem of "picking up" video fragments of the .mts file:
- Find out the frames numbers of video fragments (without commercials).
I could do it by:
a) add frames numbers to the video
ffmpeg -i <INPUT_FILE> -vf "drawtext=fontfile=Arial.ttf:text='%{frame_num}':start_number=0:x=(w-tw)/2:y=h-(2*lh):fontcolor=white:fontsize=50:" -c:a copy <OUT_FILE>
b) check the numbers in some video player
- Use MLT framework to make a movie from the original .mts file, by using the found fragments (identified by the frame numbers).
I'm not sure, which method would be the best in order to obtain the video without commercials, and with properly synchronized audio.