#!/bin/bash
#by pw 2017/03/07
#检测参数
if [[ $# != 2 && $# != 1 ]] ; then
echo "USAGE: $0 url(m3u8网络路径,支持一级嵌套但不支持多码率) tsnum(切片个数,不指定时全部下载)"
exit 1;
fi
#检测m3u8
input_m3u8=$1
if !(echo "$input_m3u8" | grep "m3u8" >/dev/null) ; then
echo "invalid m3u8!"
exit 1;
fi
main_m3u8=${input_m3u8##*/}
prefix=${input_m3u8%/*}
fold=${main_m3u8%.*}
mkdir -p $fold
#主m3u8下载
echo "downloading $main_m3u8"
wget -q $input_m3u8 -O $fold/$main_m3u8
if !([ -s $fold/$main_m3u8 ]) ;then
echo "main m3u8 download failed"
rm -f $fold/$main_m3u8
rmdir $fold
exit 1;
fi
#子m3u8下载
if cat $fold/$main_m3u8 | grep 'm3u8' >/dev/null ;then
sub_m3u8=$(cat $fold/$main_m3u8 | grep 'm3u8')
echo "downloading $sub_m3u8"
wget -q $prefix/$sub_m3u8 -O $fold/$sub_m3u8
if !([ -s $fold/$sub_m3u8 ]) ;then
echo "sub m3u8 download failed"
rm -f $fold/$main_m3u8
rm -f $fold/$sub_m3u8
rmdir $fold
exit 1;
fi
down_m3u8=$fold/$sub_m3u8
else
down_m3u8=$fold/$main_m3u8
fi
#rm -fr $fold/*.ts
#切片下载
tsnum=$2
if [[ $tsnum -ge 1 ]] ;then
for ts in $(cat $down_m3u8 |grep 'ts'|head -n $tsnum)
do
echo "downloading $ts"
wget -q $prefix/$ts -O $fold/$ts;
done
else
for ts in $(cat $down_m3u8 |grep 'ts')
do
echo "downloading $ts"
wget -q $prefix/$ts -O $fold/$ts;
done
fi
echo "Done"
网友评论