source: gs2-extensions/parallel-building/trunk/src/bin/script/ffsplit.sh@ 27642

Last change on this file since 27642 was 27642, checked in by jmt12, 11 years ago

A script I downloaded that successfully splits video files - something I was struggling to do due to ffmpegs argument order being magic of the blackest sort

  • Property svn:executable set to *
File size: 1.9 KB
Line 
1#!/bin/bash
2
3# Written by Alexis Bezverkhyy <[email protected]> in 2011
4# This is free and unencumbered software released into the public domain.
5# For more information, please refer to <http://unlicense.org/>
6
7function usage {
8 echo "Usage : ffsplit.sh input.file chunk-duration [output-filename-format]"
9 echo -e "\t - input file may be any kind of file reconginzed by ffmpeg"
10 echo -e "\t - chunk duration must be in seconds"
11 echo -e "\t - output filename format must be printf-like, for example myvideo-part-%04d.avi"
12 echo -e "\t - if no output filename format is given, it will be computed\
13 automatically from input filename"
14}
15
16IN_FILE="$1"
17OUT_FILE_FORMAT="$3"
18typeset -i CHUNK_LEN
19CHUNK_LEN="$2"
20
21DURATION_HMS=$(ffmpeg -i "$IN_FILE" 2>&1 | grep Duration | cut -f 4 -d ' ')
22DURATION_H=$(echo "$DURATION_HMS" | cut -d ':' -f 1)
23DURATION_M=$(echo "$DURATION_HMS" | cut -d ':' -f 2)
24DURATION_S=$(echo "$DURATION_HMS" | cut -d ':' -f 3 | cut -d '.' -f 1)
25let "DURATION = ( DURATION_H * 60 + DURATION_M ) * 60 + DURATION_S"
26
27if [ "$DURATION" = '0' ] ; then
28 echo "Invalid input video"
29 usage
30 exit 1
31fi
32
33if [ "$CHUNK_LEN" = "0" ] ; then
34 echo "Invalid chunk size"
35 usage
36 exit 2
37fi
38
39if [ -z "$OUT_FILE_FORMAT" ] ; then
40 FILE_EXT=$(echo "$IN_FILE" | sed 's/^.*\.\([a-zA-Z0-9]\+\)$/\1/')
41 FILE_NAME=$(echo "$IN_FILE" | sed 's/^\(.*\)\.[a-zA-Z0-9]\+$/\1/')
42 OUT_FILE_FORMAT="${FILE_NAME}-%03d.${FILE_EXT}"
43 echo "Using default output file format : $OUT_FILE_FORMAT"
44fi
45
46N='1'
47OFFSET='0'
48let 'N_FILES = DURATION / CHUNK_LEN + 1'
49
50while [ "$OFFSET" -lt "$DURATION" ] ; do
51 OUT_FILE=$(printf "$OUT_FILE_FORMAT" "$N")
52 echo "writing $OUT_FILE ($N/$N_FILES)..."
53 ffmpeg -i "$IN_FILE" -vcodec copy -acodec copy -ss "$OFFSET" -t "$CHUNK_LEN" "$OUT_FILE"
54 let "N = N + 1"
55 let "OFFSET = OFFSET + CHUNK_LEN"
56done
Note: See TracBrowser for help on using the repository browser.