youtube_get_duration.sh - randomcrap - random crap programs of varying quality
(HTM) git clone git://git.codemadness.org/randomcrap
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
youtube_get_duration.sh (762B)
---
1 #!/bin/sh
2 # get Youtube video durations.
3
4 if test "$1" = ""; then
5 echo "usage: $0 <URL>" >&2
6 exit 1
7 fi
8
9 url="$1"
10 # replace /embed/ URLs with /watch/ these more consistently contain the time in seconds.
11 url=$(printf '%s' "$url" | sed 's@/embed/@/watch/@g')
12
13 hurl "$url" | \
14 LC_ALL=C awk '
15 function timefmt(d) {
16 s = int(d);
17 m = s / 60;
18 h = s / 3600;
19 if (int(h) <= 0)
20 return sprintf("%02d:%02d", m % 60, s % 60);
21 else
22 return sprintf("%d:%02d:%02d", h, m % 60, s % 60);
23 }
24 {
25 sec = 0;
26 i = index($0, "\\\"videoDurationSeconds\\\":\\\"");
27 if (i > 0) {
28 sec = int(substr($0, i + 27, 10));
29 } else {
30 i = index($0, "\"lengthSeconds\":\"");
31 if (i > 0)
32 sec = int(substr($0, i + 17, 10));
33 }
34 if (sec > 0) {
35 print timefmt(sec);
36 exit(0);
37 }
38 }'