summaryrefslogtreecommitdiff
path: root/dot/local/bin/twmp4
blob: 3cc3dad826281eb07fa5259b72c5144fc288045f (plain)
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
#!/bin/sh
INPUT=$1
set -ex

[ -f "$INPUT" ]
case "$INPUT" in
    *.mp4) OUTPUT=${INPUT%.mp4}.twitter.mp4 ;;
    *.*) OUTPUT=${INPUT%.*}.twitter.mp4 ;;
    *) OUTPUT=${INPUT}.twitter.mp4 ;;
esac

if [ ! -e "$OUTPUT" ]
then
    # source: https://gist.github.com/nikhan/26ddd9c4e99bbf209dd7
    ffmpeg -hide_banner \
           -i "$INPUT" \
           -vcodec libx264 \
           -vf 'scale=640:trunc(ow/a/2)*2' \
           -acodec aac \
           -vb 1024k \
           -minrate 1024k -maxrate 1024k \
           -bufsize 1024k -ar 44100 \
           -strict experimental \
           -r 30 \
           "$OUTPUT"
fi

video_length()
{
    # source: https://superuser.com/questions/650291/how-to-get-video-duration-in-seconds/945604#945604
    ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$1"
}

chunk_seconds=135 # twitter max is 140, but ffmpeg can run over specified time
video_length=$(video_length "$OUTPUT")

if [ "${video_length%.*}" -gt "$chunk_seconds" ]
then
    # source: https://unix.stackexchange.com/questions/1670/how-can-i-use-ffmpeg-to-split-mpeg-video-into-10-minute-chunks/212518#212518
    CHUNKS=${OUTPUT%.mp4}.%03d.mp4
    ffmpeg -hide_banner \
           -i "$OUTPUT" \
           -c copy \
           -map 0 \
           -segment_time "$chunk_seconds" -f segment -reset_timestamps 1 "$CHUNKS"
fi