summaryrefslogtreecommitdiff
path: root/templates/bin/katsomo-dl
blob: 73d39ab6e99c98208925b169fbf55e47e13c3980 (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
#!/bin/sh

# Downloads a video from katsomo.fi. The first parameter is the stream
# URL, the optional second parameter is the output file.
#
# Because the main stream is Silverlight, which can't be downloaded,
# get the mobile stream.

if [ "x$1" = "x" ]; then
    echo "Expected URL missing" 1>&2
    exit 1
fi

OUTPUT_FILE="$2"
if [ "x$OUTPUT_FILE" = "x" ]; then
    OUTPUT_FILE="-"
fi

# Convert http://www.katsomo.fi/?progId=xxx URL into mobile site URL
# and parse the stream URL from the HTML.

MOBILE_PAGE=`echo ${1} | sed "s/www/m/"`
STREAM_URL=`curl -A iPhone -L --max-redirs 10 $MOBILE_PAGE 2>/dev/null | grep "video\/mp4" | sed "s/<.*src=\"//" | sed "s/\"\/>//" | sed "s/^ *//g"`

if [ "x$STREAM_URL" = "x" ]; then
    echo "Failed to find the stream URL in $MOBILE_PAGE" 1>&2
    exit 1
fi

# The STREAM_URL points to an m3u playlist, using avconv to download
# the stream.

if [ "x$OUTPUT_FILE" = "x-" -o -p "$OUTPUT_FILE" ]; then
    # Transcode into a streamable format
    avconv -y -i "$STREAM_URL" -f flv -vcodec libx264 -acodec libmp3lame -ab 192k -ar 22050 "$OUTPUT_FILE"
else
    # Would use -acodec copy, but due to a bug avconv sometimes fails
    # with a non-monotonic timestamp error.
    avconv -y -i "$STREAM_URL" -vcodec copy -acodec mp2 -ab 128k "$OUTPUT_FILE"
fi

exit $?