!sudo apt install ffmpeg
!pip install FFmpeg
#pip install ffmpeg-python
import datetime
import re
import threading
from queue import Queue
import requests,os
from google.colab import drive
from google.colab import files
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
import ffmpeg
drive.mount('/content/drive/')
# 预下载,获取m3u8文件,读出ts链接,并写入文档
def down():
# m3u8链接
url = 'https://cdn.605-zy.com/ppvod/38B2023DBAC50CEB41722973112E8435.m3u8'
# 当ts文件链接不完整时,需拼凑
# 大部分网站可使用该方法拼接,部分特殊网站需单独拼接
base_url = "https://cdn.605-zy.com"
#print(base_url)
resp = requests.get(url)
m3u8_text = resp.text
#print(m3u8_text)
# 按行拆分m3u8文档
ts_queue = Queue(10000)
lines = m3u8_text.split('\n')
# 找到文档中含有ts字段的行
concatfile = '/content/drive/My Drive/TS/' + "s" + '.txt'
for line in lines:
if '.ts' in line:
if 'http' in line:
# print("ts>>", line)
ts_queue.put(line)
else:
line = base_url + line
ts_queue.put(line)
# print('ts>>',line)
filename = re.search('([a-zA-Z0-9-]+.ts)', line).group(1).strip()
# 一定要先写文件,因为线程的下载是无序的,文件无法按照
# 123456。。。去顺序排序,而文件中的命名也无法保证是按顺序的
# 这会导致下载的ts文件无序,合并时,就会顺序错误,导致视频有问题。
open(concatfile, 'a+').write("file %s\n" % filename)
return ts_queue,concatfile
# 线程模式,执行线程下载
def run(ts_queue):
tt_name = threading.current_thread().getName()
while not ts_queue.empty():
url = ts_queue.get()
r = requests.get(url, stream=True)
filename = re.search('([a-zA-Z0-9-]+.ts)', url).group(1).strip()
with open('/content/drive/My Drive/TS/' + filename, 'wb') as fp:
for chunk in r.iter_content(5242):
if chunk:
fp.write(chunk)
print(tt_name + " " + filename + 'Download completed')
# 视频合并方法,使用ffmpeg
def merge(concatfile, name):
try:
path = '/content/drive/My Drive/TS/' + name + '.mp4'
command = 'ffmpeg -y -f concat -i %s -crf 18 -ar 48000 -vcodec libx264 -c:a aac -r 25 -g 25 -keyint_min 25 -strict -2 %s' % (concatfile, path)
os.system(command)
print('Successfully combined')
except:
print('Combine fail')
if __name__ == '__main__':
name = input('input name of movie:')
start = datetime.datetime.now().replace(microsecond=0)
s,concatfile = down()
threads = []
for i in range(200):
t = threading.Thread(target=run, name='th-'+str(i), kwargs={'ts_queue': s})
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
end = datetime.datetime.now().replace(microsecond=0)
print('Time consumed:' + str(end - start))
merge(concatfile,name)
over = datetime.datetime.now().replace(microsecond=0)
print('Combining:' + str(over - end))
#The following code is for combination of all the *.ts files into one
import requests,os
import re
from google.colab import drive
from google.colab import files
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
drive.mount('/content/drive/')
list=[]
f = open('/content/drive/My Drive/TS/s.txt','r')
for line in f.readlines():
line=line.replace('file ','/content/drive/My Drive/TS/').strip()
list.append('\"'+line+'\"')
f.close()
space=' '
concatfile = space.join(list)
command = 'cat %s > "/content/drive/My Drive/TS/All_Combined.ts"'%(concatfile)
os.system(command)
#Making use of FFmpeg
!sudo apt install ffmpeg
!pip install FFmpeg
#pip install ffmpeg-python
import datetime
import re
import threading
from queue import Queue
import requests,os
from google.colab import drive
from google.colab import files
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
import ffmpeg
drive.mount('/content/drive/')
!ffmpeg -f concat -i "/content/drive/My Drive/TS/s.txt" -codec copy "/content/drive/My Drive/TS/aba.ts"
#!ffmpeg -i "/content/drive/My Drive/TS/s.txt" -map 0 -c copy "/content/drive/My Drive/TS/ts.mp4"
#!ffmpeg -f concat -i "/content/drive/My Drive/TS/s.txt" -c copy aaa.ts
!ffmpeg -y -f concat -i "/content/drive/My Drive/TS/s.txt" -crf 18 -ar 48000 -vcodec libx264 -c:a aac -r 25 -g 25 -keyint_min 25 -strict -2 "/content/drive/My Drive/TS/all.mp4"
#Zip a folder on gdrive
from google.colab import drive
from google.colab import files
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
drive.mount('/content/drive/')
!zip -r "/content/drive/My Drive/TS.zip" "/content/drive/My Drive/TS"
!unzip -u "/content/drive/My Drive/TS/example.zip" -d "/content/drive/My Drive/TS/NewFolder"
-u part controls extraction only if new/necessary. It is important if suddenly you lose connection or hardware switches off.
-d creates the directory and extracted files are stored there.
import requests,os
import re
from google.colab import drive
from google.colab import files
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
drive.mount('/content/drive/')
!apt install unzip
!unzip -u "/content/drive/My Drive/TS/94.zip" -d "/content/drive/My Drive/TS/aqp"
import requests,os
import re
from google.colab import drive
from google.colab import files
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
drive.mount('/content/drive/')
list=[]
with open("/content/drive/My Drive/TS/aqp/video-94itv_app-16363410549670.m3u8") as f:
Lines = f.readlines()
for line in Lines:
if '.ts' in line:
lt='/content/drive/My Drive/TS/aqp/'+line.strip()
list.append('\"'+lt+'\"')
space=' '
concatfile = space.join(list)
print(concatfile)
command = 'cat %s > "/content/drive/My Drive/TS/All_Combined.ts"'%(concatfile)
os.system(command)