In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Python m3u8 file how to extract small video related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone after reading this Python m3u8 file how to extract small video article will have a harvest, let's take a look.
1. HLS protocol and m3u8 file
HLS, the abbreviation of HTTP Live Streaming, is a streaming media network transmission protocol based on HTTP proposed by Apple. Is part of Apple's QuickTime X and iPhone software systems. It works by dividing the entire stream into small HTTP-based files for download, only a few at a time. When the media stream is playing, the client can choose to download the same resource at different rates from many different alternate sources, allowing streaming sessions to adapt to different data rates. When starting a streaming session, the client downloads an extended M3U (m3u8) playlist file that contains metadata to find available media streams.
M3U8 is the Unicode version of M3U, encoded in UTF-8. "M3U" and "M3U8" files are the basis of Apple's HTTP Live Streaming format, which can be played on devices such as iPhone and Macbook. It is a file format for playing multimedia list, and the text content is a series of media fragment resources. If the fragment resources are played sequentially, the multimedia resources can be fully displayed. The format is roughly as follows:
# unencrypted # EXTM3U # EXT-X-VERSION:3 # EXT-X-TARGETDURATION:8 # EXT-X-MEDIA-SEQUENCE:0 # EXTINF:4.000000, 1af12fece7a000000.ts # EXTINF:4.320000, 1af12fece7a000001.ts. # EXTINF:3.800000 1af12fece7a001155.ts # EXT-X-ENDLIST # encryption # EXTM3U # EXT-X-VERSION:3 # EXT-X-TARGETDURATION:6 # EXT-X-PLAYLIST-TYPE:VOD # EXT-X-MEDIA-SEQUENCE:0 # EXT-X-KEY:METHOD=AES-128 URI= "https://ts1.yuyuangewh.com:9999/20200808/1XdSSbTb/2000kb/hls/key.key" # EXTINF:3, https://ts1.yuyuangewh.com:9999/20200808/1XdSSbTb/2000kb/hls/EUtRrqJU.ts # EXTINF:4.72, https://ts1.yuyuangewh.com:9999/20200808/1XdSSbTb/2000kb/hls/HF90vrrN.ts... # EXTINF:0.24 Https://ts1.yuyuangewh.com:9999/20200808/1XdSSbTb/2000kb/hls/b7ZLcRqT.ts # EXT-X-ENDLIST
here are some common tags in m3u8 files:
Tag format function EXTM3U#EXTM3U indicates that the file is a m3u8 file, and each m3u8 file must place the tag on the first line EXT-X-VERSIONEXT-X-VERSION: indicates that the file is a m3u8 file, and each m3u8 file must place the tag on the first line EXT-X-TARGETDURATION#EXT-X-TARGETDURATION: indicates the maximum duration of each video segment (in seconds) EXT-X-PLAYLIST-TYPE#EXT-X-PLAYLIST-TYPE: indicates the streaming media type VOD indicates that the video stream is an on-demand source, so the server cannot change the m3u8 file EVENT indicates that the video stream is a live source, so the server cannot change or delete any part of the file, but it can add new content EXT-X-MEDIA-SEQUENCE#EXT-X-MEDIA-SEQUENCE at the end of the file: indicates the sequence number of the first URL clip file in the playlist, each media clip URL has a unique integer serial number, and each media clip sequence number is increased by 1 in the order of occurrence, if the label is not specified The default sequence number starts from 0, EXT-X-KEY#EXT-X-KEY:METHOD=AES-128,URI= "http:xxxx", IV= "xxxx" indicates the encryption method of the video stream file, METHOD represents the encryption method, URI represents the key path, the key is a 16-byte data, and IV is a 128bit hexadecimal value EXTINF#EXTINF:, [] that indicates the media clip length specified by URL (in seconds). Duration can be a decimal integer or floating point, and its value must be less than or equal to the value specified by EXT-X-TARGETDURATION EXT-X-ENDLIST#EXT-X-ENDLIST indicates the end of the m3u8 file
two。 Third-party library-m3u8
m3u8 is a parser dedicated to parsing m3u8 files. For more information about the operation of the library, please see the official example.
# install m3u8 pip install m3u8# load m3u8 file import m3u8# returns a M3U8 object playlist = m3u8.load (uri=' http://videoserver.com/playlist.m3u8') # url # playlist = m3u8.load (uri='playlist.m3u8') # file print (playlist.segments) # print EXT-X-KEY label and all EXTINF tags Sign: print (playlist.target_duration) # print the value of the EXT-X-TARGETDURATION tag for key in playlist.keys: if key: # if the video file is encrypted You can view the encryption parameters print (key.uri, key.method, key.iv) 3. Synthesize mp4 files
takes this video as an example. The process is as follows:
1. Find the .m3u8 file corresponding to the video
two。 Parse the .m3u8 file to extract the url of .ts video
3. Download videos in .ts format
4. Decrypt video in .ts format (this step is not required if the video stream is not encrypted)
5. Synthesize videos in .mp4 or other formats
# step 1, I loaded the m3u8 file, or you can directly use the url playlist = m3u8.load (uri='./data/index.m3u8') corresponding to the m3u8 file # step 2, extract URL for seg in playlist.segments: print (seg.uri) # step 3 Download ts video with open ('xxxxx.ts',' wb') as f: ts = get_ts (url) f.write (ts) # step 4, decrypt cipher_text = pad (data_to_pad=cipher_text, block_size=AES.block_size) aes = AES.new (key=key, mode=AES.MODE_CBC, iv=iv) cipher_text = aes.decrypt (cipher_text) # step 5 Synthetic files = glob.glob (os.path.join ('. / video','* .ts')) for file in files: with open (file, 'rb') as fr, open ('. / video_de/baitoushan.mp4', 'ab') as fw: content = fr.read () fw.write (content) 4. Complete code #-*-coding: utf-8-*-# @ Time: 2021-5-10 20purl @ Author: XiaYouRan# @ Email: youran.xia@foxmail.com# @ File: video.py# @ Software: PyCharmfrom Crypto.Cipher import AESfrom Crypto.Util.Padding import padfrom concurrent.futures import ThreadPoolExecutorimport requestsimport m3u8import globimport osimport timeimport logginglogging.getLogger ("urllib3") .setLevel (logging.WARNING) def AESDecrypt (cipher_text, key Iv): cipher_text = pad (data_to_pad=cipher_text, block_size=AES.block_size) aes = AES.new (key=key, mode=AES.MODE_CBC, iv=iv) cipher_text = aes.decrypt (cipher_text) # clear_text = unpad (padded_data=cipher_text, block_size=AES.block_size) return cipher_textheaders = {'User-Agent':' Mozilla/5.0 (Windows NT 10.0) Win64 X64) 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'} def get_ts (url): try: response = requests.get (url) Verify=False) response.raise_for_status () response.encoding = 'utf-8' return response.content except Exception as err: print (err) return b''def save_ts (url, index): filename = os.path.join ('. / video', str (index) .zfill (5) + '.ts') with open (filename) 'wb') as f: ts = get_ts (url) f.write (ts) print (filename +' is okies') if _ _ name__ = ='_ main__': playlist = m3u8.load (uri='./data/index.m3u8') # thread pool The introduction of index can prevent video disorder during synthesis with ThreadPoolExecutor (max_workers=10) as pool: for index, seg in enumerate (playlist.segments): pool.submit (save_ts, seg.uri, index) key = get_ts (playlist.keys [- 1] .uri) files = glob.glob (os.path.join ('. / video','* .ts') for file in files: with open (file) 'rb') as fr, open ('. / video_de/baitoushan.mp4', 'ab') as fw: content = fr.read () encontent = AESDecrypt (content, key=key, iv=key) fw.write (encontent) print (file +' is okies') This is the end of the article on "how to extract small videos from m3u8 files in Python". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to extract small videos from m3u8 files in Python". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.