Created
November 13, 2012 09:10
-
-
Save anonymous/4064786 to your computer and use it in GitHub Desktop.
nrk download helper, run nrk.sh for usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<xsl:stylesheet version="1.0" | |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
xmlns:ttml="http://www.w3.org/ns/ttml" | |
xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling"> | |
<xsl:output | |
method="text" | |
indent = "no" | |
encoding="UTF-8"/> | |
<!-- | |
Usage: | |
$ subsurl=$(curl "$showurl" | awk -F'"' '/data-subtitlesurl/ {print "http://tv.nrk.no"$2;exit}') | |
$ curl "$subsurl" | xsltproc "$scriptdir/nrk-subtitles2srt.xsl" - | |
--> | |
<xsl:template match="ttml:tt"> | |
<xsl:apply-templates select="./ttml:body"/> | |
</xsl:template> | |
<xsl:template match="ttml:body"> | |
<xsl:apply-templates select="./*"/> | |
</xsl:template> | |
<xsl:template match="ttml:div"> | |
<xsl:apply-templates select="./*"/> | |
</xsl:template> | |
<xsl:template match="ttml:p"> | |
<!-- Weird indentation here because of literal newlines --> | |
<xsl:number/><xsl:text> | |
</xsl:text> | |
<xsl:call-template name="duration"/><xsl:text> | |
</xsl:text> | |
<xsl:apply-templates/><xsl:text> | |
</xsl:text> | |
</xsl:template> | |
<xsl:template match="ttml:br"> | |
<xsl:text> | |
</xsl:text> | |
</xsl:template> | |
<xsl:template match="ttml:span"> | |
<xsl:text> </xsl:text> | |
<xsl:choose> | |
<xsl:when test="./@style='italic'"><i><xsl:apply-templates select="*|text()"/></i></xsl:when> | |
<xsl:when test="./@style='bold'"><b><xsl:apply-templates select="*|text()"/></b></xsl:when> | |
<xsl:otherwise><u><xsl:apply-templates select="*|text()"/></u></xsl:otherwise> | |
</xsl:choose> | |
<xsl:text> </xsl:text> | |
</xsl:template> | |
<!-- That was the easy part, the mess below is all for creating the | |
end-timestamp of each subtitle, since the XML format only | |
specifies the start-timestamp --> | |
<xsl:template name="duration"> | |
<xsl:variable name="nowsecs"> | |
<xsl:call-template name="seconds"> | |
<xsl:with-param name="timestring" select="./@begin"/> | |
</xsl:call-template> | |
</xsl:variable> | |
<xsl:variable name="dursecs"> | |
<xsl:call-template name="seconds"> | |
<xsl:with-param name="timestring" select="./@dur"/> | |
</xsl:call-template> | |
</xsl:variable> | |
<xsl:variable name="defsecs"><xsl:value-of select="$nowsecs + $dursecs"/></xsl:variable> | |
<xsl:variable name="untilsecs"> | |
<xsl:variable name="nextsecs"> | |
<xsl:call-template name="seconds"> | |
<xsl:with-param name="timestring" select="following-sibling::p[1]/@begin"/> | |
</xsl:call-template> | |
</xsl:variable> | |
<xsl:choose> <!-- even works for last item (no following-siblings and nextsecs is NaN) --> | |
<xsl:when test="$nextsecs < $defsecs"><xsl:value-of select="$nextsecs"/></xsl:when> | |
<xsl:otherwise><xsl:value-of select="$defsecs"/></xsl:otherwise> | |
</xsl:choose> | |
</xsl:variable> | |
<xsl:variable name="nextss"><xsl:value-of select="format-number( $untilsecs mod 60, '00' )"/></xsl:variable> | |
<xsl:variable name="nextmm"><xsl:value-of select="format-number( (($untilsecs - $nextss) div 60) mod 60, '00' )"/></xsl:variable> | |
<xsl:variable name="nexthh"><xsl:value-of select="format-number( ($untilsecs - $nextss - 60*$nextmm) div 3600, '00' )"/></xsl:variable> | |
<!-- Now output (we ignore milliseconds): --> | |
<xsl:value-of select="translate(./@begin, '.', ',')"/> --> <xsl:value-of select="concat($nexthh, ':', $nextmm, ':', $nextss, ',000')"/> | |
</xsl:template> | |
<xsl:template name="seconds"> | |
<xsl:param name="timestring"/> | |
<xsl:variable name="hh"><xsl:value-of select="substring($timestring, 1,2)"/></xsl:variable> | |
<xsl:variable name="mm"><xsl:value-of select="substring($timestring, 4,2)"/></xsl:variable> | |
<xsl:variable name="ss"><xsl:value-of select="substring($timestring, 7,2)"/></xsl:variable> | |
<xsl:value-of select="$hh*3600+$mm*60+$ss"/> | |
</xsl:template> | |
</xsl:stylesheet> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#### Options handling #### | |
usage() { | |
echo "Usage: $0 [OPTIONS] SHOWURL [MANIFESTKEY]" | |
echo ""; | |
echo "Possible options:" | |
echo " -p PLAYER one of vlc, mplayer (default), umplayer, smplayer, none" | |
echo " -q QUALITY one of high (default), medium, low" | |
echo ""; | |
echo "SHOWURL looks like 'http://tv.nrk.no/serie/foo'." | |
echo "" | |
echo "If MANIFESTKEY is not included, the show will be downloaded in the background" | |
echo "with ffmpeg (requires ffmpeg installed). If it is included, it will be" | |
echo "downloaded in the background with AdobeHDS.php (requires php installed)." | |
echo "" | |
echo "MANIFESTKEY looks like 'AHIUFEEBJJXO', found in Firefox by clicking" | |
echo "Ctrl+Shift+K in Firefox (when playing SHOWURL) and filtering for" | |
echo "'manifest'. Click the link and copy the last part (after g=)." | |
echo "Including MANIFESTKEY and using AdobeHDS.php seems to give better sync" | |
echo "of subtitles." | |
exit 1; | |
} | |
quality=high | |
player=mplayer | |
OPTIND=1; while getopts "h?q:p:" opt; do | |
case $opt in | |
h|\?) echo "$OPTARG"; usage ;; | |
q) case $OPTARG in low|medium|high) quality=$OPTARG;; *) echo "ERROR: Quality should be one of low|medium|high"; usage; esac ;; | |
p) player=$OPTARG ;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
[ "$1" = "--" ] && shift | |
showurl=$1 | |
hdcore=2.7.6 # Does this ever change? | |
manifestkey=$2 | |
if [[ -z $showurl ]]; then echo "ERROR: Missing SHOWURL";echo; usage; fi | |
#### Some helpers #### | |
if [[ -n "$(readlink $0)" ]]; then | |
# don't bother recursing (readlink -f is non-POSIX) | |
scriptdir=$(dirname $(readlink $0)) | |
else | |
scriptdir=$(dirname $0) | |
fi | |
subsarg=-sub; quietarg=-quiet # used by mplayer | |
case $player in | |
vlc) subsarg=--sub-file; quietarg=--quiet ;; | |
none) player=echo; subsarg=; quietarg= ;; | |
esac | |
showtmp=$(mktemp -t nrk.XXXXXXXXXX) | |
getsubs () { | |
subsurl=$(awk -F'"' '/data-subtitlesurl/ {print "http://tv.nrk.no"$2;exit}' "$showtmp") | |
curl -s -A "$agent" "$subsurl" | xsltproc "$scriptdir/nrk-subtitles2srt.xsl" - | |
} | |
gettitle () { | |
sed -n '/akamai: {.*"title":/ s/.*"title":"\([^"]*\)".*/\1/ p' "$showtmp" | |
} | |
#### Start downloading/streaming and playing #### | |
if [[ -n $manifestkey ]]; then # Download using AdobeHDS.php: | |
agent="Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0" | |
curl -s -A "$agent" "$showurl" > "$showtmp" | |
showtitle=$(gettitle) | |
manifest=$(awk -F'="|"' '/data-media=.*manifest.f4m/ {print $2;exit}' "$showtmp")"?hdcore=$hdcore&g=$manifestkey" | |
echo "$manifest" | |
getsubs > "$showtitle.srt" | |
[[ -f "$scriptdir/AdobeHDS.php" ]] || curl -s https://raw.github.com/K-S-V/Scripts/master/AdobeHDS.php > "$scriptdir/AdobeHDS.php" | |
# Download backgrounded, but kill it if user Ctrl+C's this script: | |
php -c "$scriptdir/php.ini" "$scriptdir/AdobeHDS.php" --outfile "$showtitle.flv" --quality "$quality" --delete --manifest "$manifest" & dlpid=$! | |
trap 'kill $dlpid &>/dev/null; rm -f "$showtmp";' EXIT | |
while ps -p $dlpid &>/dev/null && [[ ! -s $showtitle.flv ]]; do | |
echo "Waiting for $showtitle.flv to appear ..."; | |
sleep 1; | |
done | |
[[ ! -s $showtitle.flv ]] && exit 1 | |
if [[ -s $showtitle.srt ]]; then | |
$player $subsarg "$showtitle.srt" "$showtitle.flv" | |
else | |
$player "$showtitle.flv" | |
fi | |
ps -p $dlpid &>/dev/null && echo && echo "Movie is still downloading (abort it with Ctrl+C)" | |
wait $dlpid | |
else # Download using ffmpeg: | |
agent="Mozilla/5.0 (iPadde; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0" # as long as it contains the substring iPad, we get the m3u8 | |
curl -s -A "$agent" "$showurl" > "$showtmp" | |
case $quality in low) quality=2;; medium) quality=3;; *) quality=4; esac | |
showtitle=$(gettitle) | |
master=$(awk -F'="|"' '/data-media=.*master\.m3u8/ {print $2;exit}' "$showtmp") | |
index=$(curl -s -A "$agent" "$master" | grep "index_${quality}_av\.m3u8" -m1) | |
echo "$index" | |
getsubs > "$showtitle.srt" | |
# Download backgrounded, but kill it if user Ctrl+C's this script: | |
ffmpeg -i "$index" -c copy "$showtitle.ts" & dlpid=$! | |
trap 'kill $dlpid &>/dev/null; rm -f "$showtmp";' EXIT | |
while ps -p $dlpid &>/dev/null && [[ ! -s $showtitle.ts ]]; do | |
echo "Waiting for $showtitle.ts to appear ..."; | |
sleep 1; | |
done | |
[[ ! -s $showtitle.ts ]] && exit 1 | |
if [[ -s $showtitle.srt ]]; then | |
$player $subsarg "$showtitle.srt" "$showtitle.ts" | |
else | |
$player "$showtitle.ts" | |
fi | |
ps -p $dlpid &>/dev/null && echo && echo "Movie is still downloading (abort it with Ctrl+C)" | |
wait $dlpid | |
fi | |
rm -f "$showtmp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[PHP] | |
extension=bcmath.so | |
extension=curl.so |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment