-
Notifications
You must be signed in to change notification settings - Fork 3
/
splitfiles.sh
executable file
·70 lines (53 loc) · 2.47 KB
/
splitfiles.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
# Short script to split videos by filesize using ffmpeg by LukeLR (taken from stack overflow: https://stackoverflow.com/questions/38259544/using-ffmpeg-to-split-video-files-by-size)
# The BAVC version has been updated to be frame accurate. It was tested on 2021-06-22 to be completely frame accurate using an FFV1/MKV file
# This script currently only works losslessly with MKV files, it needs some updates to properly handle color metadata for MOV
if [ $# -ne 2 ]; then
echo 'Illegal number of parameters. Needs at least 3 parameters:'
echo 'Usage:'
echo './split-video.sh FILE SIZELIMIT "FFMPEG_ARGS'
echo
echo 'Parameters:'
echo ' - FILE: Name of the video file to split'
echo ' - SIZELIMIT: Maximum file size of each part (in bytes)'
echo ' - FFMPEG_ARGS: Additional arguments to pass to each ffmpeg-call'
echo ' (video format and quality options etc.)'
echo ' Opttional. If nothing is entered script will streamcopy'
exit 1
fi
FFMPEG_ARGS="$3"
if [ $# -ne 3 ]; then
echo 'No FFmpeg transcode parameters entered. The script will streamcopy'
FFMPEG_ARGS="-c copy"
fi
FILE="$1"
SIZELIMIT="$2"
# Duration of the source video
DURATION=$(ffprobe -i "$FILE" -show_entries format=duration -v quiet -of default=noprint_wrappers=1:nokey=1)
# Duration that has been encoded so far
CUR_DURATION=0
# Filename of the source video (without extension)
BASENAME="${FILE%.*}"
# Extension for the video parts
EXTENSION="${FILE##*.}"
#EXTENSION="mkv"
# Number of the current video part
i=1
# Filename of the next video part
NEXTFILENAME="$BASENAME-$i.$EXTENSION"
echo "Duration of source video: $DURATION"
# Until the duration of all partial videos has reached the duration of the source video
while [[ $CUR_DURATION != $DURATION ]]; do
# Encode next part
echo ffmpeg -i "$FILE" -ss "$CUR_DURATION" -fs "$SIZELIMIT" $FFMPEG_ARGS "$NEXTFILENAME"
ffmpeg -ss "$CUR_DURATION" -i "$FILE" -fs "$SIZELIMIT" $FFMPEG_ARGS "$NEXTFILENAME"
# Duration of the new part
NEW_DURATION=$(ffprobe -i "$NEXTFILENAME" -show_entries format=duration -v quiet -of default=noprint_wrappers=1:nokey=1)
# Total duration encoded so far
CUR_DURATION=$( echo "$CUR_DURATION + $NEW_DURATION" | bc )
#CUR_DURATION=$((CUR_DURATION + NEW_DURATION))
i=$((i + 1))
echo "Duration of $NEXTFILENAME: $NEW_DURATION"
echo "Part No. $i starts at $CUR_DURATION"
NEXTFILENAME="$BASENAME-$i.$EXTENSION"
done