• 大小: 3.98KB
    文件类型: .py
    金币: 1
    下载: 0 次
    发布日期: 2024-05-10
  • 语言: Python
  • 标签: 下载  视频  

资源简介

网易云课堂的教学视频进行下载

资源截图

代码片段和文件信息

#!C:\Python3.7
# -*- coding:utf-8 -*-

import requests
import os
from threading import Thread
import threading
import optparse

sem = threading.Semaphore(100)
headers = {
    “User - Agent“: “Mozilla / 5.0(Windows NT 10.0;WOW64) AppleWebKit / 537.36(KHTML like Gecko) Chrome / 84.0.4147.105 Safari / 537.36“
    }

def down_load_ts(urlpathname):
    basepath = os.path.join(os.getcwd()pathname)
    if not os.path.exists(basepath):
        os.mkdir(basepath)


    try:
        req = requests.get(urlheaders=headers)
        #print(req.text)
        file_line_list = req.text.split(‘\n‘)
        if not “#EXTM3U“ in file_line_list[0]:
            raise baseException(u‘非M3U8的连接‘)
        else:
            unknow = True
            file_index=100000
            for indexline in enumerate(file_line_list):
                if “#EXTINF“ in line:
                    unknow = False
                    url_ts = str(url).rsplit(‘/‘1)[0] +‘/‘+ str(file_line_list[index+1])
                    #下载文件名重新编号,以防合并是造成文件顺序混乱
                    file_name = str(file_index)+‘.ts‘
                    file_index+=1
                    full_file_name =os.path.join(basepathfile_name)
                    # print(url_ts)

                    process_ts(url_ts full_file_name)

                    # t = Thread(target=process_tsargs=(url_tsfull_file_name))
                    # t.start()

            if unknow:
                raise baseException(‘未找到对应的下载连接‘)
            else:
                # t.join()
                print(pathname‘:downloaded‘)
                print(“start merge file to mp4“)
                mergeFileToMP4(pathname)
                return


    except Exception as e:
        print(e)
        return

def process_ts(url_tsfull_file_name):
    with sem:
        req_ts = requests.get(url_ts headers=headers)
        print(“downloading file:“ os.path.split(full_file_name)[1])
        with open(full_fil

评论

共有 条评论