python3--download Video According to m3u8 (under) requests

Source: Internet
Author: User

Second half ing, well, I was going to write it tomorrow. gif), please however,,, eight o ' Day in the morning to run the company overtime (reading + study) to go, found the door hung a big lock, I just want to say that access is to take a look, add what Broken lock O (╥﹏╥) O, seriously hit the staff of the overtime enthusiasm [0 ' Д ' 0], so roll back to write a blog, sadness, write a bit of code also stepped on a half-day pit (lazy mess), sadness 10086 ...

No. 7.31 morning, that is, the day of separation, wit I suddenly thought, Requests.get () Download the file is actually obtained binary content and then write to the file you created, in order to achieve the download pictures, documents, videos and so on, so, so I directly open the existing video files to add the newly acquired binary content to the back, you can more directly solve the problem of TS stream merge, wit such as I, immediately try to pay attention to the file opening and writing coding problems, sure enough,,, and then run to sign the departure agreement in the afternoon, perfect.

Operating environment: Windows and Linux (note modified under path), python36

The code is as follows:

#!/user/bin/env python#-*-coding:utf-8-*-# au:caopeiya# 201808011import os, Shutilimport urllib.request, Urllib.err Or, requests# opens and reads the Web content def geturldata (URL): try:urldata = Urllib.request.urlopen (URL, timeout=20) #. Read (). de Code (' Utf-8 ', ' Ignore ') # Urldata = Requests.get (URL, timeout=20) #. Read (). Decode (' utf-8 ', ' ignore ') retur n Urldata except Exception as Err:print (f ' err geturldata ({URL}) \ n ', err) return-1# download file-requestsdef get Down_reqursts (URL, file_path): Try:header = {"User-agent": "mozilla/5.0 (Windows NT 6.1; rv:2.0.1) gecko/20100101 firefox/4.0.1 "} response = Requests.get (URL, timeout=120, Headers=header) with open (File_path, mode= ' ab+ ') as F:f.write (response.content) # When the download file is large, use the Loop download # with open (File_path, M Ode= ' WB ') as F: # for content in Response.iter_content (1024x768): # f.write (content) print ("D    Own successful! ")    Except Exception as E:    Print (e) def getvideo_requests (url_m3u8, Path, videoname): print (' Begin run ~~\n ') Urldata = Geturldata (url_m3u8) Tempname_video = Os.path.join (path, f ' {videoname}.ts ') # f ' {} ' equals ' {} '. Format () or '%s '%videoname open (tempname_vid EO, "WB"). Close () # empty (incidentally Create) Tempname_video file, prevent stopping halfway, continue downloading repeat Write # print (Urldata) for line in Urldata: # decode Decod E ("Utf-8"), because it is directly using the crawled link content, so the need to decode by line, if the early decoding can not be used directly for the loop, will be error url_ts = str (Line.decode ("Utf-8")). Strip () # Important: Stri P (), used to clear whitespace and newline characters that exist before and after a string if not '. ts ' in url_ts:continue else:if not Url_ts.startswit H (' http '): # To determine if the string starts with ' http ', if not the URL link is not complete, you need to stitching # stitching TS stream video URL url_ts = Url_m3u8.replace ( Url_m3u8.split ('/') [-1], url_ts) print (url_ts) getdown_reqursts (Url=url_ts, file_path=tempname_video) # download view Frequency stream filename = os.path.join (path, f ' {videoname}.mp4 ') shutil.move (tempname_video, filename) print (f ' great, {Videon Ame}.mp4 finish down! ') IF __name__ = = ' __main__ ': url_m3u8 = ' http://wscdn.alhls.xiaoka.tv/201886/2f5/75a/HoHdTc1LjUaBjZbJ/index.m3u8 ' path = R ' d:\\ ' Videoname = url_m3u8.split ('/') [-2] getvideo_requests (url_m3u8, Path, videoname) # getdown_reqursts (' HT Tp://wscdn.alhls.xiaoka.tv/201886/2f5/75a/hohdtc1ljuabjzbj/147.ts ', F ' d:/videos/84.ts ')?

  

Be aware of the following points:

1. For decoding ("Utf-8") for decoding, because each loop gets the line T is ASCII encoding, must be decoded to utf-8 to become a recognizable string;

2. Use. Strip () to remove whitespace and newline characters from each line,

python3--download Video According to m3u8 (under) requests

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.