Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python Record live streams (TS FILES)

# pip install urllib
# pip install m3u8
# pip install streamlink
import urllib
import m3u8
import streamlink


def get_stream(url):
    """
    Get upload chunk url
    """
    streams = streamlink.streams(url)
    stream_url = streams["best"]

    m3u8_obj = m3u8.load(stream_url.args['url'])
    return m3u8_obj.segments[0]


def dl_stream(url, filename, chunks):
    """
    Download each chunks
    """
    pre_time_stamp = 0
    for i in range(chunks+1):
        stream_segment = get_stream(url)
        cur_time_stamp = 
            stream_segment.program_date_time.strftime("%Y%m%d-%H%M%S")

        if pre_time_stamp == cur_time_stamp:
            pass
        else:
            print(cur_time_stamp)
            file = open(filename + '_' + str(cur_time_stamp) + '.ts', 'ab+')
            with urllib.request.urlopen(stream_segment.uri) as response:
                html = response.read()
                file.write(html)
            pre_time_stamp = cur_time_stamp


url = "my-youtube-link-to-record"
dl_stream(url, "live", 15)
Comment

PREVIOUS NEXT
Code Example
Python :: Higher-order functions and operations on callable objects in python 
Python :: matplotlib annotate align center 
Python :: tkl to pkr 
Python :: python tcp 
Python :: how to add a list to a list python 
Python :: nested dict 
Python :: random email generator python 
Python :: python wikipedia 
Python :: np.all() 
Python :: pick random value from dictionary python 
Python :: return render django 
Python :: where are python libraries installed ubuntu 
Python :: print list vertically python 
Python :: python find last index of character in string 
Python :: append multiple elements python 
Python :: check how many letters in a string python 
Python :: python oops 
Python :: loop python 
Python :: id() python 
Python :: pandas to excel 
Python :: python max with custom function 
Python :: phone numbers 
Python :: how to send a command to cmd using python 
Python :: python chatbot api 
Python :: how to reverse string in python 
Python :: pandas count distinct values in column 
Python :: what does tuple mean in python 
Python :: drop null values in dataframe 
Python :: axes_style seaborn 
Python :: how to create an auto clicker in python 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =