1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
""" @File : dingtalk.py @Time : 2020/4/25 16:40 @Author : iBoy @Email : iboy@iboy.tech @Description : 钉钉中那些无法下载的网课,可以通过Python协程下载,不想在线观看,突破2倍速限制 @Software: PyCharm """
import gevent from gevent import monkey
monkey.patch_all()
import requests import os import time
download_dir = "download" course_id = "##########" auth_key = "################" total_time = "97:17"
def download_video(k): base_url = "http://dtliving-pre.alicdn.com/live_hp/{}/{}.ts?auth_key={}".format(course_id, k, auth_key) print(base_url) resp = requests.get(base_url) with open(download_dir + "/{}.ts".format(k), 'wb') as f: f.write(resp.content) f.close()
def get_max_video_num(play_time): try: play_time = play_time.split(":") total_seconds = int(play_time[0]) * 60 + int(play_time[1]) except Exception as e: print("获取视频最大数量出现异常", str(e)) max_value = total_seconds // 30 while max_value != 0: base_url = "http://dtliving-pre.alicdn.com/live_hp/{}/{}.ts?auth_key={}".format(course_id, max_value, auth_key) resp = requests.get(base_url) if resp.status_code == 404: max_value = max_value - 1 else: print("共有", max_value, "个视频") break return max_value
def merge_videos(filename): ts_list = os.listdir(download_dir) ts_list.sort(key=lambda x: int(x[:-3])) with open("video.txt", "w") as f: for name in ts_list: f.write(("file " + os.getcwd() + r'/{}/{}'.format(download_dir, name) + "\n").replace('\\', "/")) f.close() cmd = "ffmpeg -f concat -safe 0 -i video.txt -c copy {}/{}.mp4".format(download_dir, filename) os.system(cmd) for f in ts_list: try: os.remove(download_dir + "/" + f) except Exception as e: print(str(e)) os.remove("video.txt")
if __name__ == "__main__": if not os.path.exists(download_dir): print("文件夹不存在,新建文件夹download") os.mkdir(download_dir) max_video_num = get_max_video_num(total_time) time_start = time.time() download_queue = [gevent.spawn(download_video, k) for k in range(1, max_video_num + 1)] gevent.joinall(download_queue) time_end = time.time() print("下载用时:", time_end - time_start, "s") print("下载完成正在合并...") merge_videos("计算机网络") print("合并完成!")
|