Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import subprocess
- import os
- import sys
- from selenium import webdriver
- from bs4 import BeautifulSoup as bs
- def getLocalFileName(url):
- last_path = url.split('/')[-1]
- return last_path[0:last_path.find("?")]
- def download_file(url):
- local_filename = getLocalFileName(url)
- # NOTE the stream=True parameter
- r = requests.get(url, stream=True)
- with open(local_filename, 'wb') as f:
- for chunk in r.iter_content(chunk_size=1024):
- if chunk: # filter out keep-alive new chunks
- f.write(chunk)
- #f.flush() commented by recommendation from J.F.Sebastian
- return local_filename,r
- def isDownloaded(url):
- local_filename = getLocalFileName(url)
- return local_filename in os.listdir("./")
- def downloadVideos(root):
- filenames = []
- i = 1;
- is_ok = True;
- print("Downloading Videos...")
- while(is_ok):
- if not isDownloaded(root%i):
- local_filename,request = download_file(root%i)
- is_ok =request.ok
- if not is_ok:
- os.remove(local_filename)
- else:
- #print("downloaded %s" % local_filename)
- filenames.append(local_filename)
- else:
- local_filename = getLocalFileName(root%i)
- print("downloaded already %s" % local_filename)
- filenames.append(local_filename)
- i=i+1
- print("")
- return filenames
- def mergeVideos(filenames,filename):
- print ""
- print "Download process finished merging videos.. It may take a long time."
- ##merge files
- if "videos.txt" in os.listdir("./"):
- os.remove("videos.txt")
- videos_file= open("videos.txt","w")
- for video in filenames:
- videos_file.write("file '"+video+"'\n")
- videos_file.close()
- command = "ffmpeg -f concat -i videos.txt -c copy %s.mp4" % filename
- subprocess.check_output(command, shell=True)
- for video in filenames:
- os.remove(video)
- os.remove("videos.txt")
- def getRootUrl(url):
- driver = webdriver.Chrome()
- driver.set_window_size(0,0)
- driver.get(url)
- source = driver.page_source
- soup = bs(source,"html5lib")
- video = soup.find("video").find("source").get("src")
- video = video.replace("index.m3u8","1000/prog_index%d.ts")
- title = driver.title
- driver.close()
- return video,title
- def main():
- url = sys.argv[1]
- root_url,title = getRootUrl(url)
- title = title.replace(" ","_")
- filenames = downloadVideos(root_url)
- mergeVideos(filenames,title)
- print("Video downloaded and merged successfully as name:%s"%title)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement