Create a gist now

Instantly share code, notes, and snippets.

@hansdampf /_explanation.md Secret
Last active Aug 29, 2015

What would you like to do?
srf video downloader

script to download one or many videos from sf drs please use it only to save the videos for backups and don't spread those in the internet.

This is to respect the intention of sfdrs to not provide a download link, in order that the video doesn't spread.

#!/bin/sh
# script to download one or many videos from sf drs
# please use it only to save the videos for backups
# and don't spread those in the internet.
#
# This is to respect the intention of sfdrs to not
# provide a download link, in order that the video
# doesn't spread.
# This is free and unencumbered software released into the public domain.
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# For more information, please refer to <http://unlicense.org/>
if [ "$#" -ne 1 ]; then
echo "specify url with links to videos (overview) or link to detail page (single video)"
exit 1
fi
rtmpdump -h 2>/dev/null || { echo >&2 "rtmpdump is required, please install from here: http://rtmpdump.mplayerhq.hu/download/"; exit 1; }
# all is ok from here
# if provided url is detail page, fetch only one id
if [[ $1 == */video/* ]]; then
echo "fetching videos from detail page"
arrIN=(${1//id=/ })
id=${arrIN[1]}
ids=($id)
else
echo "fetching video from result page"
ids=($(curl -s "$1" | grep -E -o "id=([0-9a-z]+-[0-9a-z\-]+)" | sort | uniq | cut -c 4-))
fi
## loop over all videos found on page
for i in ${ids[@]}; do
video_url=$(curl -s "http://www.srf.ch/webservice/cvis/segment/$i/.json" | grep -E -o "rtmp:[^\"]+" | head -1 | sed "s/\\\\//g")
if [ -n "$video_url" ]; then
echo fetching $video_url
file_name=$(basename $video_url)
status=1
while [ $status -ne 0 ]; do
# strangely sometimes rtmpdump leaves empty files => delete those to avoid endless loop
if [ ! -s $file_name ]; then rm -f $file_name; fi
rtmpdump -e -r $video_url -o $file_name
status=$?
done
fi
done

Thanks, seems to work!

Just one approvement:
grep -E -o "rtmp:[^"]+"
takes the first mentioned rtmp-URL, or am I wrong?

As I saw, there are often several rtmp-URLs with different qualities in the json, beginning with the lowest quality, ascending to the best one. So it would be better to take the last mentioned rtmp-URL.

Two examples I found:

JSON for a478be63-b411-44ab-a8f3-b6c0a99b540c:

rtmp://cp50792.edgefcs.net/ondemand/cosf/vod/ts20/2008/09/ts20_20080916_124619_106k.flv
...
rtmp://cp50792.edgefcs.net/ondemand/cosf/vod/ts20/2008/09/ts20_20080916_124619_500k.flv

.

JSON for 6129573d-49c4-4e35-9a20-b79e07710a68:

rtmp://cp50792.edgefcs.net/ondemand/mp4:aka/vod/ts13/2014/02/ts13_20140218_124500_v_webcast_h264_q10.mp4
...
rtmp://cp50792.edgefcs.net/ondemand/mp4:aka/vod/ts13/2014/02/ts13_20140218_124500_v_webcast_h264_q20.mp4
...
rtmp://cp50792.edgefcs.net/ondemand/mp4:aka/vod/ts13/2014/02/ts13_20140218_124500_v_webcast_h264_q30.mp4
...
rtmp://cp50792.edgefcs.net/ondemand/mp4:aka/vod/ts13/2014/02/ts13_20140218_124500_v_webcast_h264_q40.mp4
...
rtmp://cp50792.edgefcs.net/ondemand/mp4:aka/vod/ts13/2014/02/ts13_20140218_124500_v_webcast_h264_q50.mp4

Thanks for your work!

EDIT: Just saw: The order of the URLs can change. So you'll better look for the filename ending. My quick&dirty fix:
grep -E -o "rtmp:[^"]+((q50.mp4)|(500k.flv))"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment