程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python+ffmpeg realizes multi-threaded downloading, decryption, merging into MP4 and output of TS video in m3u8 file.

編輯:Python

demand : Download the movie from the target website to the local .

Target site movies :《 Adventure in the jungle 》 Watch the movie online for free _ Apocalypse cinema

Analysis website :

1, According to web page analysis Of the film m3u8 It's on the index.m3u8 in , There are two in the web page index.m3u8, Choose the second .( How to analyze a web page , I won't discuss it here )

download m3u8 file

# download m3u8
def down_m3u8(url):
resp = requests.get(url, headers)
text = resp.text
with open(" Adventure in the jungle .m3u8", "w") as f:
f.write(text)
print(" Adventure in the jungle .m3u8 Download complete ")

2, Yes, downloaded m3u8 analysis , Here you will find that there is a process AES-128 encryption ,ts The video will not play after downloading , So you need to decrypt .

Using multi thread download ts video

# Use thread pool to download m3u8 Inside ts
def down_ts():
with open("./ Adventure in the jungle .m3u8", "r", encoding="utf-8") as f:
# n = 0
with ThreadPoolExecutor(10) as t:
for n, line in enumerate(f):
line = line.strip()
if line.startswith("#"):
continue
t.submit(ts_video, n, line)
# download ts video D:\\data\\movie_ts\\m1 Download for ts Video storage path
def ts_video(n, line):
# Download Video
resp3 = requests.get(line)
with open(f"D:\\data\\movie_ts\\m1\\{n}.ts", "wb") as f:
f.write(resp3.content)
print(n, " Download complete ")

3, The video downloaded here is encrypted , Can't watch directly in the player , The next step is to encrypt the downloaded data ts Decrypt the video

  Decrypt ts video

# Yes, download t well ts Decrypt the video D:\\data\\video\\m1\\ After decryption ts Storage path
def jiemi():
f = open("./ Adventure in the jungle .m3u8", "r", encoding="utf-8")
keys = []
for file in f:
key = re.findall(r'URI="(.*?)"', file) # Match with regular key
if key:
keys.append(key[0])
key_url = keys[0]
resp4 = requests.get(key_url)
key = resp4.text
key = key.encode("utf-8")
aes = AES.new(key=key, IV=b"0000000000000000", mode=AES.MODE_CBC)
download_path = "D:\\data\\movie_ts\\m1"
all_ts = os.listdir(download_path)
li = []
for i in all_ts:
i = i.split(".")[0]
li.append(int(i))
li.sort()
for i in li:
with open(f"D:\\data\\movie_ts\\m1\\{i}.ts", mode="rb") as f1, \
open(f"D:\\data\\video\\m1\\2000{i}.ts", mode="wb") as f2:
bs = f1.read()
f2.write(aes.decrypt(bs))
print(f" The first {i} Video processing finished ")

Decrypt okay ts video

  Decrypt okay ts Video can be viewed directly with the player

4, Good for decryption ts For video ffmpeg synthesis MP4 video

You need to install ffmpeg 

Windows:

1. Get into http://ffmpeg.org/download.html#build-windows, Click on windows The corresponding icon , Enter the download interface and click download Download button ,
2. Unzip download okay zip File to specified directory
3. Put the extracted files in the directory bin Catalog ( contain ffmpeg.exe ) Added to the path In the environment variables
4. DOS Command line input ffmpeg -version, The following interface appears to indicate that the installation is complete :

Mac ( Open the terminal (Terminal), use homebrew install ):

brew install ffmpeg --with-libvorbis --with-sdl2 --with-theora

Linux:

apt-get install ffmpeg libavcodec-extra

ffmpeg synthesis MP4 video  

# Good for decryption ts For video ffmpeg synthesis MP4 video
def movie_video():
filePath = "D:\\data\\video\\m1"
file_list = os.listdir(filePath)
li = []
for file in file_list:
file = file.split(".")[0]
li.append(int(file))
li.sort()
ts_file_name = []
for i in li:
i = str(i) + ".ts"
ts_file_name.append(i)
with open("D:\\data\\video\\m1\\file_list.txt", "w+") as f:
for file in ts_file_name:
f.write("file '{}'\n".format(file))
print("file_list.txt The generated ")
txt_file = "D:\\data\\video\\m1\\file_list.txt"
mp4 = "D:\\data\\movies\\ Adventure in the jungle .mp4"
cmd = f"ffmpeg -f concat -i {txt_file} -c copy {mp4}"
try:
os.system(cmd)
except Exception as e:
print(e)
print("done")

All reference codes are as follows :

import re
import time
import os
import requests
from Crypto.Cipher import AES
from concurrent.futures import ThreadPoolExecutor
# download m3u8
def down_m3u8(url):
resp = requests.get(url, headers)
text = resp.text
with open(" Adventure in the jungle .m3u8", "w") as f:
f.write(text)
print(" Adventure in the jungle .m3u8 Download complete ")
# Use thread pool to download m3u8 Inside ts
def down_ts():
with open("./ Adventure in the jungle .m3u8", "r", encoding="utf-8") as f:
# n = 0
with ThreadPoolExecutor(10) as t:
for n, line in enumerate(f):
line = line.strip()
if line.startswith("#"):
continue
t.submit(ts_video, n, line)
# download ts video
def ts_video(n, line):
# Download Video
resp3 = requests.get(line)
with open(f"D:\\data\\movie_ts\\m1\\{n}.ts", "wb") as f:
f.write(resp3.content)
# n += 1
print(n, " Download complete ")
# Yes, download t well ts Decrypt the video
def jiemi():
f = open("./ Adventure in the jungle .m3u8", "r", encoding="utf-8")
keys = []
for file in f:
key = re.findall(r'URI="(.*?)"', file)
if key:
keys.append(key[0])
key_url = keys[0]
resp4 = requests.get(key_url)
key = resp4.text
key = key.encode("utf-8")
aes = AES.new(key=key, IV=b"0000000000000000", mode=AES.MODE_CBC)
download_path = "D:\\data\\movie_ts\\m1"
all_ts = os.listdir(download_path)
li = []
for i in all_ts:
i = i.split(".")[0]
li.append(int(i))
li.sort()
for i in li:
with open(f"D:\\data\\movie_ts\\m1\\{i}.ts", mode="rb") as f1, \
open(f"D:\\data\\video\\m1\\2000{i}.ts", mode="wb") as f2:
bs = f1.read()
f2.write(aes.decrypt(bs))
print(f" The first {i} Video processing finished ")
# Good for decryption ts For video ffmpeg synthesis MP4 video
def movie_video():
filePath = "D:\\data\\video\\m1"
file_list = os.listdir(filePath)
li = []
for file in file_list:
file = file.split(".")[0]
li.append(int(file))
li.sort()
ts_file_name = []
for i in li:
i = str(i) + ".ts"
ts_file_name.append(i)
with open("D:\\data\\video\\m1\\file_list.txt", "w+") as f:
for file in ts_file_name:
f.write("file '{}'\n".format(file))
print("file_list.txt The generated ")
txt_file = "D:\\data\\video\\m1\\file_list.txt"
mp4 = "D:\\data\\movies\\ Adventure in the jungle .mp4"
cmd = f"ffmpeg -f concat -i {txt_file} -c copy {mp4}"
try:
os.system(cmd)
except Exception as e:
print(e)
print("done")
if __name__ == '__main__':
url = "https://pps.sd-play.com/20211127/g8V4A0hE/1000kb/hls/index.m3u8"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36",
}
t_start = time.time()
print("------------------ Start the download m3u8----------------------------")
down_m3u8(url)
time.sleep(1)
print("------------------ Start the download ts video ----------------------------")
down_ts()
time.sleep(1)
print("------------------ Start downloading and decrypting ts video ----------------------------")
jiemi()
time.sleep(1)
print("------------------ Start downloading synthesis MP4----------------------------")
movie_video()
t_end = time.time()
t = t_end-t_start
print(t)

Tips : When the code runs to Pictured Shown , Here is in the composite video , It takes time , Just wait patiently for the completion .

 python Xiaobai is one , No joy, no spray. . I hope that's helpful ......

Last ,python Dafa is good !

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved