diff options
Diffstat (limited to 'dot/local/bin')
-rwxr-xr-x | dot/local/bin/twmp4 | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/dot/local/bin/twmp4 b/dot/local/bin/twmp4 index 3cc3dad..755c67b 100755 --- a/dot/local/bin/twmp4 +++ b/dot/local/bin/twmp4 | |||
@@ -1,19 +1,57 @@ | |||
1 | #!/bin/sh | 1 | #!/bin/bash |
2 | |||
3 | usage() | ||
4 | { | ||
5 | cat <<EOF | ||
6 | Usage: $0 [options] <input> | ||
7 | |||
8 | Options: | ||
9 | --help|--usage|-h | ||
10 | --start <time> | ||
11 | --end <time> | ||
12 | EOF | ||
13 | } | ||
14 | |||
15 | [ $# -gt 0 ] || set -- --help | ||
16 | ARGS=$(getopt -o 'h' --long 'usage,help,start:,end:' -n 'twmp4' -- "$@") || exit | ||
17 | eval set -- "$ARGS" | ||
18 | unset ARGS | ||
19 | |||
20 | INPUT_OPTIONS= | ||
21 | while [ $# -gt 0 ] | ||
22 | do | ||
23 | case "$1" in | ||
24 | -h|--help|--usage) usage; exit ;; | ||
25 | --start) INPUT_OPTIONS="$INPUT_OPTIONS -ss $2"; OUTPUT_SUFFIX="${OUTPUT_SUFFIX:+$OUTPUT_SUFFIX.}$2"; shift;; | ||
26 | --end) INPUT_OPTIONS="$INPUT_OPTIONS -to $2"; OUTPUT_SUFFIX="${OUTPUT_SUFFIX:+$OUTPUT_SUFFIX.}$2"; shift;; | ||
27 | --) shift; break ;; | ||
28 | -*) usage; exit 1 ;; | ||
29 | *) break;; | ||
30 | esac | ||
31 | shift | ||
32 | done | ||
33 | |||
2 | INPUT=$1 | 34 | INPUT=$1 |
3 | set -ex | 35 | set -ex |
4 | 36 | ||
5 | [ -f "$INPUT" ] | 37 | [ -f "$INPUT" ] |
38 | |||
39 | OUTPUT_SUFFIX=${OUTPUT_SUFFIX:+$OUTPUT_SUFFIX.}twitter.mp4 | ||
40 | |||
6 | case "$INPUT" in | 41 | case "$INPUT" in |
7 | *.mp4) OUTPUT=${INPUT%.mp4}.twitter.mp4 ;; | 42 | *.mp4) OUTPUT=${INPUT%.mp4}.$OUTPUT_SUFFIX ;; |
8 | *.*) OUTPUT=${INPUT%.*}.twitter.mp4 ;; | 43 | *.*) OUTPUT=${INPUT%.*}.$OUTPUT_SUFFIX ;; |
9 | *) OUTPUT=${INPUT}.twitter.mp4 ;; | 44 | *) OUTPUT=${INPUT}.$OUTPUT_SUFFIX ;; |
10 | esac | 45 | esac |
11 | 46 | ||
47 | # source: https://gist.github.com/nikhan/26ddd9c4e99bbf209dd7 | ||
48 | |||
12 | if [ ! -e "$OUTPUT" ] | 49 | if [ ! -e "$OUTPUT" ] |
13 | then | 50 | then |
14 | # source: https://gist.github.com/nikhan/26ddd9c4e99bbf209dd7 | ||
15 | ffmpeg -hide_banner \ | 51 | ffmpeg -hide_banner \ |
52 | $INPUT_OPTIONS \ | ||
16 | -i "$INPUT" \ | 53 | -i "$INPUT" \ |
54 | $OUTPUT_OPTIONS \ | ||
17 | -vcodec libx264 \ | 55 | -vcodec libx264 \ |
18 | -vf 'scale=640:trunc(ow/a/2)*2' \ | 56 | -vf 'scale=640:trunc(ow/a/2)*2' \ |
19 | -acodec aac \ | 57 | -acodec aac \ |
@@ -27,7 +65,6 @@ fi | |||
27 | 65 | ||
28 | video_length() | 66 | video_length() |
29 | { | 67 | { |
30 | # source: https://superuser.com/questions/650291/how-to-get-video-duration-in-seconds/945604#945604 | ||
31 | ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$1" | 68 | ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$1" |
32 | } | 69 | } |
33 | 70 | ||