[Part 3] HLS - hls everywhere - Download by python3 code how?




Code python3 (file :"main.py")

You can buil with
$ pyinstaller --onefile main.py




//
# pyinstaller --onefile main.py
import hls_me
from datetime import datetime
import argparse
import os, sys
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--file", required=True, help="file or URL m3u8")
ap.add_argument("-d", "--directory", required=False, help="directory to save video")
ap.add_argument("-n", "--name", required=False, help="name of file video")
ap.add_argument("-t", "--thread", required=False, help="number of thread downloader", type=int)
ap.add_argument("-r", "--referer", required=False, help="headers referer assumed")
ap.add_argument("-p", "--prefix", required=False, help="prefix of URL in file m3u8")

now = datetime.now() # current date and time

args = vars(ap.parse_args())
if not (args["thread"]):
    args["thread"]= 8
if not (args["directory"]):
    args["directory"]= "{}".format(os.getcwd())
if not (args["name"]):
    args["name"]= "video-{}.mp4".format(now.strftime("%m-%d-%Y(%H-%M-%S)"))
if not (args["referer"]):
    args["referer"]= ''
if not (args["prefix"]):
    args["prefix"]= ""
print("file or URL m3u8: '{}'".format(args["file"]))
print("directory to save video: '{}'".format(args["directory"]))
print("name of file video: '{}'".format(args["name"]))
print("number of thread downloader: '{}'".format(args["thread"]))
print("headers referer assumed: '{}'".format(args["referer"]))
print("prefix of URL in file m3u8: '{}'".format(args["prefix"]))
try:
    #raise Exception('spam', 'bug')
    hls_me.hls_fetch(
        args["file"], #'g1.m3u8', # 'https://.....m3u8
        args["directory"], #'C:/Users/Admin/Documents/PyProject/hlsMe/temp', # directory save file
        args["name"], # '1.mp4', # name of file save
        int(args["thread"]), # 10, # number of thread downloader
        args["referer"], # 'https://avgle.com/', # fake headers referer
        args["prefix"]  # prefixURL
    )
except Exception as g:
    print(g)
//

Usage:
python main.py -f https://mnmedias.api.telequebec.tv/m3u8/29880.m3u8 (Do not download immediately but will stop (contains a variant stream))
python main.py -f https://s2.content.video.llnw.net/smedia/.................m3u8
python main.py -f file1.m3u8

Next: sometime


variant_m3u8 = m3u8.loads('#EXTM3U ... contains a variant stream ...')

for iframe_playlist in variant_m3u8.iframe_playlists:
    iframe_playlist.uri
    iframe_playlist.iframe_stream_info.bandwidth

Comments