【Python小随笔】TS视频下载

def ts_download(base_url,start,end):
    """
    base_url: 下载地址
    start :   开始文件名
    end :     结束文件名
    """
    import requests
    for i in range(start, end + 1):
        # 4个字符,0补充空位,I为变化的数字
        file_num = f"{i:04}.ts"
        url = base_url + file_num
        # 获取响应
        response = requests.get(url)
        if response.status_code == 200:
            with open('./mp4/' + file_num, 'wb') as f:
                f.write(response.content)
        print(f"Downloaded {file_num}")


if __name__ == '__main__':
    # 原视频后面会跟上001.ts 
    base_url = "https://m3u8.com/video/m3u8/560fba1a/" 
    ts_download(base_url,3,106)
posted @ 2024-08-09 22:56  PythonNew_Mr.Wang  Views(23)  Comments(0Edit  收藏  举报