下载m3u8文件并合成的shell脚本

#!/bin/bash

if [ -z $1 ];
then
	echo Usage $0 m3u8 url
fi

m3u8_url=$1
m3u8_file=/tmp/m3u8.$$
https_file=/tmp/https.$$

# 下载m3u8文件
wget -q  -O $m3u8_file   ${m3u8_url}     

# 从m3u8文件中提取ts连接
cat $m3u8_file | awk '{ if(index($0, "https://") > 0) {print $0} }' > $https_file

echo removeing ts files
rm out*.ts -f 2> /dev/null

# 定义头部信息
headers="--header='Host: cdn-2-dx.cdnpan.com' --header='User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'  --header='Accept: */*' --header='Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2' --header='Accept-Encoding: gzip, deflate, br' --header='Origin: https://baiduyunbo.com'  --header='Connection: keep-alive'"

# 遍历ts连接,并一个个下载
count=1
while read url;
do
	echo downloading $url
	out_file=$(printf "%03d"  $count)
	# wget -q $headers  -t 0 $url;
	wget -q -t 0 -O out${out_file}.ts $url;
	let count+=1;
done< $https_file

# 合成ts片断文件。合成后的文件就是一个连贯的能够播放的文件
echo merge ts files
cat out*.ts > all_$$.ts

# 最终删除临时文件
rm $m3u8_file  $https_file -f 2> /dev/null

echo Done!