Cutting a video into segments on the command-line

So, today I needed to cut a video into several segments and figured that as in the future I may need to reprocess the best thing to do would be to write a small script on the command-line to do this. Fortunately it turned out to be pretty easy… First create a file called cut_points with the points (in seconds):

5
10
20
100

(That last line of 100 is some value greater than the length of the video). Then using the following bash one-liner:

i=1;
prev=0;
for new in `cat cut_points`; do
  avconv -y -i out.mp4 -ss $prev -t $(echo "$new-$prev" | bc) -async 1 -strict experimental $i.mp4; i=$(($i+1));
  prev=$new;
done

Unfortunately this does reencode the video (I guess if you just try doing copy for the a/v streams it will only do it to the nearest B-frame so you’ll only have your cut points accurate to the nearest few seconds which wouldn’t work in my case)

Leave a Reply

Your email address will not be published. Required fields are marked *