~dricottone/my-utils

ref: ef6d4cd1b378cc0ebbcb6e1460b3909f0cdd71b6 my-utils/videos/thumbnail -rwxr-xr-x 2.0 KiB
ef6d4cd1Dominic Ricottone Incomplete work on hardware/media utils 4 days ago
                                                                                
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash

name="thumbnail"
version="1.0"
read -r -d '' help_message <<-EOF
	Extract a thumbnail from a video
	Usage: thumbnail [OPTIONS] TARGET [TARGET..]
	Options:
	 -h, --help            print this message
	 -q, --quiet           suppress error messages and prompts
	 -t, --timestamp       timestamp to extract thumbnail from
	 -v, --verbose         show additional messages
	 -V, --version         print version number and exit
EOF

source /usr/local/lib/mylib.bash

positional=()
quiet=
timestamp="00:00:10.000"
verbose=
while [[ $# -gt 0 ]]; do
  case "$1" in

  -h|--help)
    help_msg
    shift
    ;;

  -q|--quiet)
    debug_msg "Setting quiet option to 1 (was ${quiet})"
    quiet=1
    shift
    ;;

  -t|--time|--timestamp)
    debug_msg "Setting timestamp option to '${2}' (was '${timestamp}')"
    timestamp="$2"
    shift; shift
    ;;

  -v|--verbose)
    debug_msg "Setting verbose option to 1 (was ${verbose})"
    verbose=1
    shift
    ;;

  -V|--version)
    version_msg
    ;;

  *)
    debug_msg "Argument '${1}' added to positional array"
    positional+=("$1")
    shift
    ;;
  esac
done

if [[ "${#positional[@]}" -eq 0 ]]; then
  usage_msg
fi

printf "$timestamp" | grep -e '^[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9]$' >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
  error_msg "Not a valid timestamp ('${timestamp}') - must be 'HH:MM:SS.sss'"
fi

code=0
for filename in "${positional[@]}"; do
  if [[ ! -f "$filename" ]]; then
    nonfatal_error_msg "No such file '$filename'"
    code=1
  elif [[ "$filename" = *.png ]]; then
    nonfatal_error_msg "Skipping file '$filename' - already an image"
  else
    dirname="$(dirname "$filename")"
    basename="$(basename "$filename")"
    basestub="${basename%.*}"
    debug_msg "ffmpeg -i '${basename}' -hide_banner -loglevel error -ss '${timestamp}' -frames:v 1 -update 1 '${basestub}.png'"
    (cd "$dirname" && ffmpeg -i "$basename" -hide_banner -loglevel error -ss "$timestamp" -frames:v 1 -update 1 "$basestub.png")
  fi
done

exit "$code"