没有记录,就没有发生。

0%

m3u8下载脚本及其他

中国大学MOOC不久前把他们的视频从mp4换到m3u8了,为了更好的学习,有了以下脚本,基于那谁的改的来着,算了这不重要。

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
#!/usr/bin/env python3
import os
import re
import sys
import argparse
import threading


def clean_name(name, count):
file_type = name.split('.')[-1]
name = ''.join(name.split('.')[:-1])
name = re.sub('[.\/:*?"<>|-]', '', name)
name = re.sub('[0-9]', '', name)
return '%s_%s.%s'%(str(count).zfill(3), name, file_type)


class FFMPEG_thread(threading.Thread):

def __init__(self, name):
threading.Thread.__init__(self)
self.name = name

def run(self):
print('Thread %s start!' % self.name)
global dl_pool
while True:
try:
name, url = dl_pool.pop()
os.system('ffmpeg -i "{}" -c copy -bsf:a aac_adtstoasc "{}"'\
.format(url,os.path.join(video_dir,name)))
except:
print('Thread %s end!' % self.name)
break


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Download m3u8 to mp4')
parser.add_argument('-l', default=r'', help='视频列表')
parser.add_argument('-d', default=r'', help='下载目录')
parser.add_argument('-c', default=8, help='线程数,默认8')
args = parser.parse_args()
video_list = os.path.abspath(args.l)

if args.d == '':
video_dir = os.path.dirname(video_list)
else:
video_dir = os.path.abspath(args.d)

count = 1
dl_pool = []

with open(video_list, encoding = 'utf-8') as f:
lines = f.readlines()
lines = [_.strip() for _ in lines if _ != '\n']
for line in lines:
name,url = line.split('\t')
name = clean_name(name, count)
print(name,url)
dl_pool.append([name, url])
count += 1

tp = [FFMPEG_thread(i) for i in range(args.c)]
[t.start() for t in tp]
[t.join() for t in tp]

默认的效果大概也能看吧,就是对中文字体有点不满意,考完试换掉它。最后还是祝大家2020快乐啦,虽然在2019肯定没人看。

------------- END OF FILE meow~-------------