summaryrefslogtreecommitdiff
path: root/tatort-dl
blob: 71bd4652dfaed0dd396a7a1cf3d9bccc73b6b3ff (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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
# tatort-dl - download movies from ARDmediathek using rtmpdump
# Copyright (C) 2010-2012 Robin Gareus <robin@gareus.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

RTMPDUMP=${RTMPDUMP:-rtmpdump}
INTERACTIVE=
GETURLONLY=
QUIET=

while getopts ":Vhigq" opt; do
  case $opt in
    V)
      echo "tator-dl v2013-01-28"
      echo
      echo "Copyright (C) 2010-2013 Robin Gareus"
      echo "This is free software; see the source for copying conditions.  There is NO"
      echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
      exit 0
      ;;
    h)
      echo "tatort-dl - CLI tool for downloading films from http://www.ardmediathek.de/. "
      echo
      echo "Usage: tatort-dl [-i] [-g] [-q] <ARDmediathek-URL> [OutFileName] "
      echo
      echo "Wrapper script to parse rtmp and mp4 URLs from the ard-mediathek and launch"
      echo "a download using 'rtmpdump'."
      echo "The default out-file name is ./format<ard-uid>.flv"
      echo "-i option: interactive mode - ask before taking any action."
      echo "-g option: get URL - just print rtmp URL and exit"
      echo "-q option: quiet, inhibit usual output"
      echo
      echo "Environment:"
      echo "RTMPDUMP=rtmpdump"
      echo
      echo "Example:"
      echo "tatort-dl \"http://www.ardmediathek.de/ard/servlet/content/3517136?documentId=3701294\""
      echo
      echo "Report bugs to <robin@gareus.org>."
      echo "Website http://gareus.org/wiki/tatort-dl"
      exit 0
      ;;
    q)
      QUIET=yes
      shift;
      ;;
    g)
      GETURLONLY=yes
      QUIET=yes
      shift;
      ;;
    i)
      INTERACTIVE=yes
      shift;
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

URL=$(echo -e "$1" | grep "^http")
OUTFILE=$2

RTMPDUMPOPTS=""
#RTMPDUMPOPTS+=" --live"
#RTMPDUMPOPTS+=" --resume"
#RTMPDUMPOPTS+=" --quiet"

if test -z "$URL" ; then
  echo -e "Error: missing or invalid parameters\n\nUsage:\n $0 <ARDmediathek-URL> [out-file]\nThe deafult out-file name is ./format<ard-uid>.f4v\n\nExample:\n $0 \"http://www.ardmediathek.de/ard/servlet/content/3517136?documentId=3701294\"\n" >&2;
  exit 1
fi

[[ -z "$QUIET" ]] && echo -en "curl \"$URL\" .."
RTMPURLS=$(curl -s "$URL" | grep -i rtmp) 


if test -z "$RTMPURLS" ; then 
  echo  -e "\nError: no 'rtmp://' URLs were found on the given Page.\n" >&2
  exit 1;
fi

COUNT=$(echo -e "$RTMPURLS" | wc -l)
[[ -z "$QUIET" ]] && echo -e " ..found $COUNT RTMP URL(s)."

urldecode(){
  echo -e "$(sed 'y/+/ /; s/%/\\x/g')"
}

FLASH=$(echo "$RTMPURLS" | grep flashvars | tail -n1) 
if test -n "$FLASH"; then

  RTMPURL=$(echo -ne "$FLASH" \
            | sed 's/^.*streamer=\([^&]*\)&.*$/\1/g' \
            | urldecode)
  PLAYPATH=$(echo -ne "$FLASH" \
             | sed 's/^.*file=\([^&]*\)&.*$/\1/g' \
             | urldecode)
  #echo "RTMP: $RTMPURL"
  #echo "FILE: $PLAYPATH"

else 

  # try high-qualty first
  PARAM=$(echo "$RTMPURLS" | grep Web-L) 
  # fall-back to medium-qualty 
  if test -z "$PARAM"; then
    PARAM=$(echo "$RTMPURLS" | grep Web-M) 
  fi
  # 2nd fall-back: use any rtmp URL 
  if test -z "$PARAM"; then
    PARAM=$(echo "$RTMPURLS" | tail -n 1 ) 
  fi

  RTMPURL=$(echo -ne "$PARAM" | sed 's/^.*"\(rtmp[t]*:[^"]*\)",.*$/\1/g')
  PLAYPATH=$(echo -ne "$PARAM" | sed 's/^.*"\(mp4:[^"]*\)"[),].*$/\1/g')
fi

test -z "$RTMPURL" && RTMPURL="rtmp://vod.daserste.de/ardfs/"

if test -n "$GETURLONLY"; then
  PLAYPATH2=$(echo -ne "$PLAYPATH" | sed 's/^mp4://g')
  echo -n "$RTMPURL$PLAYPATH2"
  exit
fi

if test -z "$OUTFILE"; then
  OUTFILE=$(echo -ne "$PLAYPATH" | sed 's/^mp4:.*\/\([^\/?]*\)\?.*$/\1/g')
  OUTFILE=$(echo -ne "$OUTFILE" \
            | sed 's/^.*\///g' \
            | sed 's/\?.*$//g' \
            | sed 's/mp4://g' \
            | sed 's/\.f4v$//' \
            | sed 's/\.flv$//'\
           )
  OUTFILE="${OUTFILE}.flv"
fi

if test -z "$QUIET"; then
  echo
  echo "Parameters:"
  echo " RTMP-URL : $RTMPURL"
  echo " Playpath : $PLAYPATH"
  echo " Saving to: $OUTFILE"
  echo -n " PWD      : "
  pwd
  echo
fi

if test -z "$PLAYPATH -o -z "$OUTFILE ; then
 echo "Error: empty playpath or blank output filename." >&2
 exit 1
fi

if test -e "$OUTFILE"; then
  echo "WARNING: output file "$OUTFILE" exists." >&2
  if test -n "$INTERACTIVE"; then
    echo -n "[A]bort/[d]elete it/[r]esume? [A/d/r]? "
    read -n 1 VAL
    echo
    if test "$VAL" == "d"; then 
      rm -i "$OUTFILE";
    elif test "$VAL" == "r"; then 
      RTMPDUMPOPTS+=" --resume"
    else
      exit;
    fi
  else # NON-INTERACTIVE
    echo "Error: file exists. bailing out."
    exit
  fi
fi

if test -n "$INTERACTIVE"; then
  echo "rtmpdump command-line:"
  echo " #$RTMPDUMP $RTMPDUMPOPTS -o \"$OUTFILE\" --playpath \"$PLAYPATH\" -r $RTMPURL"
  echo

  echo -n " Start Download ? [Y/n] "
  read -n 1 VAL
  echo
  if test "$VAL" == "n" -o "$VAL" == "N";  then 
    exit;
  fi
fi

$RTMPDUMP $RTMPDUMPOPTS -o "$OUTFILE" --playpath "$PLAYPATH" -r $RTMPURL

if test -z "$QUIET"; then
  echo
  ls -l "$OUTFILE"
fi