爬取网络视频-未完待续

'''
下载电影
https://www.ai66.cc/zhanzhengpian/14490.html
'''
import os
import time
RESPATH = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'res')
import requests
from urllib import parse
from requests.packages.urllib3.exceptions import InsecureRequestWarning
class Movie:

    def download_m3u8_files(self,url):
        r = requests.get(url)
        lines = r.text.split('\n')
        tss = []
        for line in lines:
            if line.endswith('.ts'):
                tss.append(parse.urljoin('https://www.nmgxwhz.com:65',line))
            return

    def heBingTsVideo(self,download_path, hebing_path):
        all_ts = os.listdir(download_path)
        with open(hebing_path, 'wb+') as f:
            for i in range(len(all_ts)):
                ts_video_path = os.path.join(download_path, all_ts[i])
                f.write(open(ts_video_path, 'rb').read())
        print("合并完成!!")

    def down_tss(self,tss,name):
        # 下载ts文件
        global RESPATH
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
        }
        file_name = os.path.join(RESPATH,name)
        if os.path.exists(file_name):
            return
        os.makedirs(file_name)
        all = len(tss)
        for idx,url in enumerate(tss):
            print('{}/{}'.format(idx+1,all))
            downsize = 0
            startTime = time.time()
            requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
            req = requests.get(url, headers=headers, stream=True, verify=False)
            with(open(file_name+'/'+str(idx) + '.ts', 'wb')) as f:
                for chunk in req.iter_content(chunk_size=10000):
                    if chunk:
                        f.write(chunk)
                        downsize += len(chunk)
                        line = 'downloading %d KB/s - %.2f MB, 共 %.2f MB'
                        line = line % (
                            downsize / 1024 / (time.time() - startTime), downsize / 1024 / 1024, downsize / 1024 / 1024)
                        # print(line)
    def run(self):
        name = '金刚川'
        # m3u8_url = 'https://www.nmgxwhz.com:65/20201209/vq2N4ajW/1200kb/hls/index.m3u8'
        # tss = self.download_m3u8_files(m3u8_url)
        # self.down_tss(tss, name)

        self.heBingTsVideo(os.path.join(RESPATH,name), os.path.join(RESPATH,name+'.mp4'))
if __name__ == '__main__':
    Movie().run()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值