ffmpeg_splitter.sh 1.3 KB
Newer Older
1 2
#!/bin/bash

3
# dirty little script to split a video into 15 minutes parts with ten
4
# seconds overlap (for upload to youtube)
Milan Santosi's avatar
Milan Santosi committed
5 6
# Author: milan <milan.santosi@gmail.com>
#
7
# USAGE: read comments and adjust where necessary, then:
Milan Santosi's avatar
Milan Santosi committed
8
# ./ffmpeg_splitter.sh <inputfile>
9 10 11
####################################################################


Milan Santosi's avatar
Milan Santosi committed
12
# use globbing to get basename and extension.
13 14 15 16 17 18
# see http://linuxgazette.net/18/bash.html
fname=$1           # the file name
bname=${fname%%.*} # the file basename
ext=${fname#*.}    # the file extension


19 20 21 22 23 24 25 26 27 28
# to avoid async a/v, re-encode with every frame as keyframe
if 
    test -f $bname-keyframes.$ext
then
    :
else 
    ffmpeg -g 1 -i $1 $bname-keyframes.$ext
fi    
    

Milan Santosi's avatar
Milan Santosi committed
29 30 31 32 33 34
# get length of the original video, calculate amount of segments and
# combined length of all parts (with overlap)
let vidlen=$(ffprobe -show_format $bname-keyframes.$ext | grep duration | cut -d "." -f 1 | cut -d "=" -f 2)
let segcount=$vidlen/900
let withoverlap=$vidlen+$segcount*10
let newsegcount=$withoverlap/900
35 36


Milan Santosi's avatar
Milan Santosi committed
37 38
# loop until it's done (actually one more time, just to make sure)
for (( i = 1; i <= $newsegcount+1; i++))
39 40 41
do                                                                     
    ffmpeg -vcodec copy -acodec copy -ss $[i*890 - 890] -t 899.999 -i $bname-keyframes.$ext $bname-part0$i.$ext
done