ffmpeg chopping up a movie into chunks
I have a great little script that makes a new movie file from chopped up chunks of a list of movies, (thanks to help from this forum) except for two problems.
The problem is the first chopped portion always has a delay before playing, can be anything from 0.5 seconds to 4 or 5 seconds. I think this has something to do with key frames? The second problem is although I select, say a 10 second length of each chopped out portion of movie, this can vary from chunk to chunk so they could be 4 seconds long or even 20 seconds long. Here's the command line: Code:
ffmpeg -ss 100 -t 60 -y -i movie.mp4 -c copy -map 0 -segment_time 10 -f segment -reset_timestamps 1 output%03d.mp4 Code:
-rwxrwxrwx 1 charlie charlie 3.8M Jul 5 18:55 output000.mp4 I think I'm missing a parameter but cannot figure out what it is. Help please? |
As I suspected this was due to not setting key frame, and after a lot of testing, trial and error and help from google I've come up with an ffmpeg command that can do this all in one.
It cuts, letterboxes, sets a key frame and then chops the cut part up into chunks of equal size and no freezing of the first chunk. Here's the command: Code:
ffmpeg -ss $STARTAT -y -i $FF -vcodec libx264 -r 25 -filter:v "scale=(iw*sar)*min(640/(iw*sar)\,360/ih):ih*min(640/(iw*sar)\,360/ih), pad=640:360:(640-iw*min(640/iw\,360/ih))/2:(360-ih*min(640/iw\,360/ih))/2" -b:v 500k -acodec aac -strict experimental -ss $CHOPLENGTH -t $DURATION -g $CHOPLENGTH -sc_threshold 0 -force_key_frames "expr:gte(t,n_forced*$CHOPLENGTH)" -map 0 -segment_time $CHOPLENGTH -f segment -reset_timestamps 1 CHOPPED-%03d.mp4 $STARTAT starting point eg 200 seconds $FF a movie eg XXX.mp4 $CHOPLENGTH eg 15 seconds $DURATION length of movie to chop into chunks eg 60 seconds So this would result in 4 chunks of 15 seconds each from 200 seconds into the movie pretty coll huh! |
#1 that's still an improperly chopped and stringed mpeg. what you have now is a damaged mpeg movie. it's not a solution it's a hack.
mark it as solved then? this forum is not for presenting code "you figured out" that others have not asked for it is for asking others |
Quote:
Who made you judge and jury ?. |
Quote:
|
Anyway, besides X-LFS-2010 comment, I found my "hack" still creates a movie that freezes, not to start with but some point through the video.
So a bit more research I reread https://trac.ffmpeg.org/wiki/Concatenate and added a bit to my script to convert my chopped up files to an intermediate .ts format with this command: Code:
ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts Code:
ffmpeg -f mpegts -i "concat:temp1|temp2" -c copy -bsf:a aac_adtstoasc output.mp4 So far this seems to work, just a bit more testing with a load more movie files. Then I'm going to see if I can convert the original movies to ts after cutting, letterboxing and adding some time flags. It's an amazing tool ffmpeg and I feel I'm just scratching the surface. |
Quote:
So step 1 is to cut, letter box, and flag the input files to .mp4 container Code:
ffmpeg -ss 100 -y -i test.mp4 -t 120 -ss 13 -vcodec libx264 -r 25 -filter:v "scale=(iw*sar)*min(640/(iw*sar)\,360/ih):ih*min(640/(iw*sar)\,360/ih), pad=640:360:(640-iw*min(640/iw\,360/ih))/2:(360-ih*min(640/iw\,360/ih))/2" -b:v 500k -acodec aac -strict experimental -g 13 -sc_threshold 0 -force_key_frames "expr:gte(t,n_forced*13)" CLB.mp4 Code:
ffmpeg -y -i CLB.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -map 0 -segment_time 13 -f segment -reset_timestamps 1 CHOPPED-test-%03d.ts Code:
for movies in CHOPPED-*.ts Code:
for i in {0..9} Seems to work, files playback correctly, but I need to do more extensive testing, so it's not solved yet! |
Quote:
|
keyframes and groups of pictures. You would probably need to re-encode the whole video with iframes (everyframe is a keyframe), then chunk that out. Or other such trickery. In either case, to break it up mid group-of-pictures will require re-encoding the video. Which is an imperfect copy with compression and artifacts. The file sizes differ because the movement differs, as lots of movement does not compress well.
|
Quote:
ffmpeg -ss $ss -y -i $FF -r 29.97 -vcodec libx264 -r 29.97 -filter:v "$LETTERBOX_PARM" -b:v ${VideoBitRate} -acodec aac -strict experimental -ss $CHOPLENGTH -t $t -g $CHOPLENGTH -sc_threshold 0 -force_key_frames "expr:gte(t,n_forced*$CHOPLENGTH)" -map 0 -segment_time $CHOPLENGTH -f segment -reset_timestamps 1 CHOPPED-$CHOPLENGTH-${FF::-4}-%03d.mp4 and LETTERBOX_PARM="scale=(iw*sar)*min($width/(iw*sar)\,$height/ih):ih*min($width/(iw*sar)\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2" This is done in a loop through many videos, where FF is the video name eg zxc.mp4, or zxc.wmv and so on Then the script renames the videos to a six digit random number so that when joined up again it's a new movie of chunks in random order, like the shuffle effect on a cd-player. Here is where I just could not get an "ffmpeg concat" script to work, so I resorted to mmcat.sh which can be found here: https://trac.ffmpeg.org/wiki/mmcat Usage is Code:
mmcat.sh file1.mp4 file2.mp4 file3.mp4 outputfile.mp4 So that's it for now, post solved |
All times are GMT -5. The time now is 07:41 PM. |