Python Crawl video website m3u8 video, download. ts suffix file, merge into whole video

Source: Internet
Author: User



Recently discovered some websites, can parse each big video website VIP. Think carefully, this is also a reptile, crawling is video data.



First select a video site, I chose the film encyclopedia, and then choose the film soon released "a show."


Analysis page


I'm using a Chrome browser, F12 into the view. Select the doc in the network and find the data from the main section obtained from this website.









Enter this link in the address bar to jump to the playback page of the video source.



Of course, you can watch the video directly on this page, but we need to download the video.


Find video files


Still the previous page, and in other we found something strange.






Look, m3u8 is a thing.



M3U8 is Apple introduced a video playback standard, is a m3u, but the encoding is Utf-8, is a file retrieval format, the video is cut into a small segment of a small segment of the TS format video files, and then exist in the server (now in order to reduce the number of I/O access, typically in the memory of the server), Parse out the path through m3u8 and then go to request.



It's clear, this is what we're looking for.



Click Response to view this. m3u8 file. It was observed that the file address of the. TS suffix is regular. We only need to download all the. ts suffix files and then integrate them into one file.


Merging. ts files


Command line: "Copy/b f:\f\*.ts E:\f\new.ts".



After executing the command, all TS files in the f:\f directory are merged into a new.ts file (your original heap of files still exists).



Here, the file merge function of the Copy command is used to merge TS files, and the/b parameter after copy indicates that the files are merged in binary format, and if this parameter is not added, the target will be merged as a text file and unnecessary tags will be added to the file, which causes the playback error, so the/b parameter must be added.


scripting, downloading. ts files
 
 
from urllib import request
import urllib
from time import sleep
import socket

class CatchVideo(object):
    def __init__(self):
        self.headers = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36"
        self.url = ""

    def set_url(self, i):
        if i < 1000:
            self.url = "https://cdn.letv-cdn.com/20180811/YLDUgCD6/1000kb/hls/DtrOg2412%03d.ts" % i
        else:
            self.url = "https://cdn.letv-cdn.com/20180811/YLDUgCD6/1000kb/hls/DtrOg2412%04d.ts" % i

    # 获取并下载ts文件
    def dl_ts(self, i):
        rq = request.Request(self.url)
        rq.add_header(‘User-Agent‘, self.headers)
        response = request.urlopen(rq)
        resread = response.read()
        with open(str(i)+".ts", "wb") as f:
            f.write(resread)
        response.close()# 关闭urlopen方法,防止被ban

    def start_work(self):
        for i in range(0, 1563+1):
            self.set_url(i)
            try:
                self.dl_ts(i)
                print(str(i) + ".ts  success")
                sleep(1)
            except urllib.error.URLError as e:
                print(e.reason)
                break
            except socket.timeout as e2:
                print(e2.reason)
                self.dl_ts(i)


if __name__ == ‘__main__‘:
    catch_video = CatchVideo()
    socket.setdefaulttimeout(20)
    catch_video.start_work()





During the operation, there were two errors, namely:


    • Urllib.error.URLError: [Winerror 10054] The remote host forced the shutdown of an existing connection
    • Socket.timeout Read timeout


Workaround:



1. Add Response.close and close the Urlopen method.



2. Increase Time.sleep, with one second buffer time



3. Set the Socket.setdefaulttimeout to reserve the buffer time for the socket


There are still problems.


Script execution is slightly less efficient during actual operation. After that, you will add multiple threads, continue to improve, and increase operational efficiency.











Reference blog:





80377424



77164521









Python Crawl video website m3u8 video, download. ts suffix file, merge into whole video


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.