Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import requests
  2. import subprocess
  3. import os
  4. import sys
  5. from selenium import webdriver
  6. from bs4 import BeautifulSoup as bs
  7.  
  8. def getLocalFileName(url):
  9. last_path = url.split('/')[-1]
  10. return last_path[0:last_path.find("?")]
  11.  
  12. def download_file(url):
  13. local_filename = getLocalFileName(url)
  14. # NOTE the stream=True parameter
  15. r = requests.get(url, stream=True)
  16. with open(local_filename, 'wb') as f:
  17. for chunk in r.iter_content(chunk_size=1024):
  18. if chunk: # filter out keep-alive new chunks
  19. f.write(chunk)
  20. #f.flush() commented by recommendation from J.F.Sebastian
  21. return local_filename,r
  22.  
  23. def isDownloaded(url):
  24. local_filename = getLocalFileName(url)
  25. return local_filename in os.listdir("./")
  26.  
  27. def downloadVideos(root):
  28. filenames = []
  29. i = 1;
  30. is_ok = True;
  31. print("Downloading Videos...")
  32. while(is_ok):
  33. if not isDownloaded(root%i):
  34. local_filename,request = download_file(root%i)
  35. is_ok =request.ok
  36. if not is_ok:
  37. os.remove(local_filename)
  38. else:
  39. #print("downloaded %s" % local_filename)
  40. filenames.append(local_filename)
  41. else:
  42. local_filename = getLocalFileName(root%i)
  43. print("downloaded already %s" % local_filename)
  44. filenames.append(local_filename)
  45.  
  46. i=i+1
  47. print("")
  48. return filenames
  49.  
  50.  
  51. def mergeVideos(filenames,filename):
  52. print ""
  53. print "Download process finished merging videos.. It may take a long time."
  54. ##merge files
  55. if "videos.txt" in os.listdir("./"):
  56. os.remove("videos.txt")
  57. videos_file= open("videos.txt","w")
  58. for video in filenames:
  59. videos_file.write("file '"+video+"'\n")
  60. videos_file.close()
  61.  
  62. command = "ffmpeg -f concat -i videos.txt -c copy %s.mp4" % filename
  63. subprocess.check_output(command, shell=True)
  64.  
  65. for video in filenames:
  66. os.remove(video)
  67.  
  68. os.remove("videos.txt")
  69.  
  70. def getRootUrl(url):
  71. driver = webdriver.Chrome()
  72. driver.set_window_size(0,0)
  73. driver.get(url)
  74. source = driver.page_source
  75. soup = bs(source,"html5lib")
  76. video = soup.find("video").find("source").get("src")
  77. video = video.replace("index.m3u8","1000/prog_index%d.ts")
  78. title = driver.title
  79. driver.close()
  80. return video,title
  81.  
  82. def main():
  83. url = sys.argv[1]
  84. root_url,title = getRootUrl(url)
  85. title = title.replace(" ","_")
  86. filenames = downloadVideos(root_url)
  87. mergeVideos(filenames,title)
  88. print("Video downloaded and merged successfully as name:%s"%title)
  89.  
  90.  
  91. if __name__ == "__main__":
  92. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement