def getUrl(data): with infoLock: if(data['type'] == "youtube"): print 'Locating URL for: ' + data['id'] try: video = None if data.has_key('url'): return data['url'] else: video = pafy.new(data['id']) if data['format'] == "audio": bestaudio = video.getbestaudio(preftype="m4a") return bestaudio.url best = video.getbest(preftype="mp4") print "\n".join(["Type: %s Resolution: %s Audio: %s Video: %s" % (x._info['ext'], x.resolution, x._info['acodec'], x._info['vcodec']) for x in video._allstreams]) print "Chosen: ", "Type: %s Resolution: %s Audio: %s Video: %s" % (best._info['ext'], best.resolution, best._info['acodec'], best._info['vcodec']) if data['format'] == "high": return best.url for stream in video.streams: if stream is not best: if stream.extension == 'mp4': return stream.url return best.url except Exception as e: print "Error fetching video URL ", e data['title'] = "ERROR loading video" data['description'] = e.message.split(':')[-1:] return "error" return None
def updateVideoData(data): with infoLock: if(data['type'] == "youtube"): print 'Loading data for ' + data['id'] try: video = pafy.new(data['id']) data.update(video._ydl_info) if data['format'] == "audio": bestaudio = video.getbestaudio(preftype="m4a") return bestaudio.url best = video.getbest(preftype="mp4") print "\n".join(["Type: %s Resolution: %s Audio: %s Video: %s" % (x._info['ext'], x.resolution, x._info['acodec'], x._info['vcodec']) for x in video._allstreams]) print "Chosen: ", "Type: %s Resolution: %s Audio: %s Video: %s" % (best._info['ext'], best.resolution, best._info['acodec'], best._info['vcodec']) if data['format'] == "high": data['url'] = best.url else: for stream in video.streams: if stream is not best: if stream.extension == 'mp4': data['url'] = stream.url break else: data['url'] = best.url print data except Exception as e: print "Error fetching video data ", e data['title'] = "ERROR fetching video data" data['description'] = e.message.split(':')[-1:] return data
def runOnce(self): """ Create pafy objects for tests. """ if hasattr(Test, "hasrun"): return Test.quick = os.environ.get("quick") Test.videos = VIDEOS if not Test.quick else [] Test.playlists = PLAYLISTS if not Test.quick else [] for video in Test.videos: time.sleep(0 if Test.quick else self.delay) video['pafy'] = pafy.new(video['identifier']) video['streams'] = video['pafy'].streams video['best'] = video['pafy'].getbest() video['bestaudio'] = video['pafy'].getbestaudio() video['bestvideo'] = video['pafy'].getbestvideo() # get urls for age restricted vids if video['pafy'].videoid == "07FYdnEawAQ": _ = video['pafy'].streams[0].url _ = video['pafy'].streams[1].url_https del _ for playlist in Test.playlists: playlist['fetched'] = pafy.get_playlist(playlist['identifier']) Test.hasrun = True
def test_pafy_download(self): """ Test downloading. """ callback = lambda a, b, c, d, e: 0 vid = pafy.new("DsAn_n6O5Ns", gdata=True) vstream = vid.audiostreams[-1] name = vstream.download(callback=callback) self.assertEqual(name[0:5], "WASTE")
def listParser(list_url, q, progressQ): final_url, l = crawl(list_url) uset = set(final_url) print("Playlist fetched. Would retrieve download urls now...") # print(uset) i = 0 l = len(uset) linkArr = [] for u in uset: url = u video = pafy.new(url) s = video.getbest() fname = re.sub(r'[<>:\"\/\\|\?\*]+', "_", s.title)+"."+s.extension linkArr.append({ 'url':s.url, 'title':s.title, 'ext':s.extension, 'filename':fname }) i = i+1 progressQ.put(int(i*100/l)) q.put(linkArr)
def listParser(list_url, q, progressQ): final_url, l = crawl(list_url) uset = set(final_url) # print("Please hold on...") # print(uset) i = 0 l = len(uset) linkArr = [] for u in uset: url = u video = pafy.new(url) s = video.getbest() fname = re.sub(r'[<>:\"\/\\|\?\*]+', "_", s.title)+"."+s.extension # print(s.title) # print(fname) linkArr.append({ 'url':s.url, 'title':s.title, 'ext':s.extension, 'filename':fname }) # print(s.resolution, s.url) i = i+1 # pb["value"] = int(i*100/l) progressQ.put(int(i*100/l)) # print("Done: " + '{0:.2f}'.format(i*100/l) + " %") # print("Finished Getting List...") q.put(linkArr)
def test_pafy_download_resume(self): """ Test resuming a partial download. """ tempname = "WASTE 2 SECONDS OF YOUR LIFE-DsAn_n6O5Ns-141.m4a.temp" with open(tempname, "w") as ladeeda: ladeeda.write("abc") vid = pafy.new("DsAn_n6O5Ns", gdata=True, basic=False, signature=False) vstream = vid.audiostreams[-1].download(meta=True, remux_audio=True) name = "WASTE 2 SECONDS OF YOUR LIFE.m4a" self.assertEqual(12880, os.stat(name).st_size) # test fetching attributes vid._title = None _ = vid.title vid._rating = None _ = vid.rating vid._author = None _ = vid.author vid._rating = None _ = vid.rating vid._length = None _ = vid.length vid._viewcount = None _ = vid.viewcount vid._thumb = None _ = vid.thumb vid._length = None _ = vid.duration
def runOnce(self): """ Create pafy objects for tests. """ if hasattr(Test, "hasrun"): return Test.quick = os.environ.get("quick") Test.videos = VIDEOS if not Test.quick else [] Test.playlists = PLAYLISTS if not Test.quick else [] for video in Test.videos: time.sleep(0 if Test.quick else self.delay) video['pafy'] = pafy.new(video['identifier']) video['streams'] = video['pafy'].streams video['best'] = video['pafy'].getbest() video['bestaudio'] = video['pafy'].getbestaudio() # get urls for age restricted vids if video['pafy'].videoid == "07FYdnEawAQ": _ = video['pafy'].streams[0].url _ = video['pafy'].streams[1].url_https del _ for playlist in Test.playlists: playlist['fetched'] = pafy.get_playlist(playlist['identifier']) Test.hasrun = True
def listParser(list_url, q, progressQ): final_url, l = crawl(list_url) uset = set(final_url) print("Playlist fetched. Would retrieve download urls now...") # print(uset) i = 0 l = len(uset) linkArr = [] for u in uset: url = u video = pafy.new(url) s = video.getbest() fname = re.sub(r'[<>:\"\/\\|\?\*]+', "_", s.title) + "." + s.extension linkArr.append({ 'url': s.url, 'title': s.title, 'ext': s.extension, 'filename': fname }) i = i + 1 progressQ.put(int(i * 100 / l)) q.put(linkArr)
def createDictFromDF(df): res = dict() for url in df["URL"]: youtubeObj = pafy.new(url) ID = youtubeObj.videoid res[ID] = url return res
def download(self): logger.info('Downloading videos...') id_lst = self.get_vid_ids(self.download_num) used = [] i = 0 while i < self.download_num: index = random.randint(0, len(id_lst) - 1) if index not in used: used.append(index) video_id = id_lst[index]['id'] video = pafy.new(video_id) duration = datetime.strptime(video.duration, '%H:%M:%S') if 20 > duration.minute > 0: interval = generate_interval(video, duration) logger.info('Interval: ' + str(interval)) self.interval_lst.append(interval) cmd = [ 'runp', 'Downloaders.py', 'download_video:' + str(video_id) ] p = subprocess.Popen(cmd) pid = utils.wait_timeout(p, config.DOWNLOAD_TIMEOUT) if pid is not None: logger.info('download_video ran successfully!') store( self.id, 'https://www.youtube.com/watch?v=' + str(video.videoid), 'vid') i += 1 else: logger.info('download_video timed out!')
def test_pafy_download_resume(self): """ Test resuming a partial download. """ tempname = "WASTE 2 SECONDS OF YOUR LIFE-DsAn_n6O5Ns-171.ogg.temp" with open(tempname, "w") as ladeeda: ladeeda.write("abc") vid = pafy.new("DsAn_n6O5Ns", gdata=True, basic=False) vstream = vid.audiostreams[-1].download(meta=True, remux_audio=True) name = "WASTE 2 SECONDS OF YOUR LIFE.ogg" self.assertEqual(22675, os.stat(name).st_size) # test fetching attributes vid._title = None _ = vid.title vid._rating = None _ = vid.rating vid._author = None _ = vid.author vid._rating = None _ = vid.rating vid._length = None _ = vid.length vid._viewcount = None _ = vid.viewcount vid._thumb = None _ = vid.thumb vid._length = None _ = vid.duration
def POST(self): data = json.load(StringIO(web.data())) video = pafy.new(data['id']) dfolder = expanduser(config.conf.get('download-folder', "~/Downloads")) ensure_dir(dfolder) dfile = os.path.join(dfolder, video.title + ".mp4") best = video.getbest(preftype="mp4") best.download(filepath=dfile)
def test_pafy__invalid_win_filename(self): """ Test Windows and colon character in video name. """ os.name = "nt" youtube_title = "#$&#@)($&)(*_)&@#()&#@$_*#(&@" vid = pafy.new("http://www.youtube.com/watch?v=6vaX2P5R3VI") audio = vid.getbestaudio() expected = "#$&#@)($&)(__)&@#()&#@$__#(&@.m4a" self.assertEqual(expected, audio.generate_filename())
def test_lazy_pafy(self): """ Test create pafy object without fetching data. """ vid = pafy.new("DsAn_n6O5Ns", basic=False, signature=False) self.assertEqual(vid.bigthumb, '') self.assertEqual(vid.bigthumbhd, '') self.assertIsInstance(vid.likes, int) self.assertIsInstance(vid.dislikes, int)
def download_from_youtube(url): ''' Baixa o video do youtube e retorna o nome do arquivo (filename) ''' vPafy = pafy.new(url) play = vPafy.getbest() play.download(play.filename) return play.filename
def test_pafy__invalid_win_filename(self): """ Test Windows and colon character in video name. """ os.name = "nt" vid = pafy.new("http://www.youtube.com/watch?v=K-TNJSBrFEk") audio = vid.getbestaudio() expected = ("Jon Meacham, _Thomas Jefferson_ the Art of Power__ Autho" "rs at Google.m4a") self.assertEqual(expected, audio.generate_filename())
def test_pafy_download_resume(self): """ Test resuming a partial download. """ tempname = "WASTE 2 SECONDS OF YOUR LIFE-DsAn_n6O5Ns-171.ogg.temp" with open(tempname, "w") as ladeeda: ladeeda.write("abc") vid = pafy.new("DsAn_n6O5Ns", gdata=True) vstream = vid.audiostreams[-1].download(meta=True) name = "WASTE 2 SECONDS OF YOUR LIFE.ogg" self.assertEqual(22675, os.stat(name).st_size)
def movement(): url = str(media.get()) if url[-5:] == 'video': cap = cv2.VideoCapture(url) elif url == '0': cap = cv2.VideoCapture(0) elif url == '': cap = cv2.VideoCapture("test.mp4") else: try: video = pafy.new(url) best = video.getbest(preftype="mp4") cap = cv2.VideoCapture() cap.open(best.url) except: print( "An incorrect youtube link or an invalid source might be uploaded!\nInstead watch the prototype." ) cap = cv2.VideoCapture("test.mp4") # cap = cv2.VideoCapture(0) ret, frame1 = cap.read() ret, frame2 = cap.read() while cap.isOpened(): diff = cv2.absdiff(frame1, frame2) gray = cv2.cvtColor(diff, cv2.COLOR_BGRA2GRAY) blur = cv2.GaussianBlur(gray, (5, 5), 0) _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY) dilated = cv2.dilate(thresh, None, iterations=3) contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # cv2.drawContours(frame1, contours, -1, (0, 255, 0) ,2) for contour in contours: (x, y, w, h) = cv2.boundingRect(contour) if cv2.contourArea(contour) < 700: continue cv2.rectangle(frame1, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(frame1, "Status : {}".format("Movement Detected"), (20, 20), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 3) cv2.imshow("Feed", frame1) frame1 = frame2 ret, frame2 = cap.read() if cv2.waitKey(40) == 27: break cv2.destroyAllWindows() cap.release()
def __init__(self, link=0, youtubelink=False): if (youtubelink and link != 0): pf = pafy.new(link) best = pf.getbest(preftype="mp4") link = best.url self._windowManager = WindowManager('Cameo', self.onKeyPress) self._captureManager = CaptureManager(cv2.VideoCapture(link), self._windowManager, False) self._filter = filters.BlurFilter()
def test_generate_filename_with_meta(self): p = pafy.new('jrNLsC_Y9Oo') a = p.getbestaudio() filename = a.generate_filename(meta=True) self.assertEqual(filename, 'Jessie J - WILD (Official) ft. Big Sean' ', Dizzee Rascal-jrNLsC_Y9Oo-171.ogg') self.assertEqual(a.threed, False) self.assertEqual(a.title, 'Jessie J - WILD (Official) ft. Big Sean' ', Dizzee Rascal') self.assertEqual(a.notes, '') self.assertEqual(a.filename, 'Jessie J - WILD (Official) ft. Big Sean' ', Dizzee Rascal.ogg')
def __init__(self, url="https://www.youtube.com/watch?v=KM3S51xVS8c", rawVideoDir="./dataset/video/raw/"): self.url = url self.youtubeObj = pafy.new(url) self.rawVideoDir = rawVideoDir self.ID = self.youtubeObj.videoid self.rawVideo = self.youtubeObj.getbest(preftype="mp4") streams = self.youtubeObj.streams for s in streams: print(s.resolution, s.extension) if not os.path.exists(rawVideoDir): os.makedirs(rawVideoDir)
def test_generate_filename_with_meta(self): p = pafy.new('jrNLsC_Y9Oo') a = p.getbestaudio() filename = a.generate_filename(meta=True) self.assertEqual( filename, 'Jessie J - WILD (Official) ft. Big Sean' ', Dizzee Rascal-jrNLsC_Y9Oo-171.ogg') self.assertEqual(a.threed, False) self.assertEqual( a.title, 'Jessie J - WILD (Official) ft. Big Sean' ', Dizzee Rascal') self.assertEqual(a.notes, '') self.assertEqual( a.filename, 'Jessie J - WILD (Official) ft. Big Sean' ', Dizzee Rascal.ogg')
def test_generate_filename_with_meta(self): """ Use meta argument to generate filename. """ if Test.quick: return p = pafy.new('jrNLsC_Y9Oo', size=True) a = p.getbestaudio() filename = a.generate_filename(meta=True) self.assertEqual(filename, 'Jessie J - WILD (Official) ft. Big Sean' ', Dizzee Rascal-jrNLsC_Y9Oo-141.m4a') self.assertEqual(a.threed, False) self.assertEqual(a.title, 'Jessie J - WILD (Official) ft. Big Sean' ', Dizzee Rascal') self.assertEqual(a.notes, '') self.assertEqual(a.filename, 'Jessie J - WILD (Official) ft. Big Sean' ', Dizzee Rascal.m4a')
def listParser(list_url): final_url, l = crawl(list_url) uset = set(final_url) print("Please hold on...") # print(uset) fHTML = open("temp.html","w") i = 0 l = len(uset) for u in uset: url = u video = pafy.new(url) s = video.getbest() # print(s.resolution, s.url) fHTML.write("<a href='"+s.url+"'>" + video.title + "</a><br>") i = i+1 print("Done: " + '{0:.2f}'.format(i*100/l) + " %") fHTML.close()
def listParser(list_url): final_url, l = crawl(list_url) uset = set(final_url) print("Please hold on...") # print(uset) fHTML = open("temp.html", "w") i = 0 l = len(uset) for u in uset: url = u video = pafy.new(url) s = video.getbest() # print(s.resolution, s.url) fHTML.write("<a href='" + s.url + "'>" + video.title + "</a><br>") i = i + 1 print("Done: " + "{0:.2f}".format(i * 100 / l) + " %") fHTML.close()
def getVideoDetails(pageURL): video = pafy.new(pageURL) det = [] for s in video.streams: size = int(round(s.get_filesize()/1024/1024)) print(s.resolution, s.extension, size) # REMOVE RESTRICTED CHARACTERS FROM TITLE & ADD EXTENSION fname = re.sub(r'[<>:\"\/\\|\?\*]+', "_", s.title)+"."+s.extension # print(fname) det.append({ 'url':s.url, 'title':s.title, 'ext':s.extension, 'filename':fname, 'resolution':s.resolution, 'size':size }) return det
def getUrl(data): if(data['type'] == "youtube"): print 'Locating URL for: ' + data['id'] try: video = pafy.new(data['id']) if data['format'] == "audio": bestaudio = video.getbestaudio(preftype="m4a") return bestaudio.url best = video.getbest(preftype="mp4") if data['format'] == "high": return best.url for stream in video.streams: if stream is not best: if stream.extension == 'mp4': return stream.url return best.url except: print "Error fetching video URL" return None
def getVideoDetails(pageURL): video = pafy.new(pageURL) det = [] for s in video.streams: size = int(round(s.get_filesize() / 1024 / 1024)) print(s.resolution, s.extension, size) # REMOVE RESTRICTED CHARACTERS FROM TITLE & ADD EXTENSION fname = re.sub(r'[<>:\"\/\\|\?\*]+', "_", s.title) + "." + s.extension # print(fname) det.append({ 'url': s.url, 'title': s.title, 'ext': s.extension, 'filename': fname, 'resolution': s.resolution, 'size': size }) return det
def runOnce(self): if hasattr(Test, "hasrun"): return Test.quick = os.environ.get("quick") Test.videos = VIDEOS if not Test.quick else [] Test.playlists = PLAYLISTS if not Test.quick else [] for video in Test.videos: time.sleep(0 if Test.quick else self.delay) video['pafy'] = pafy.new(video['identifier']) video['streams'] = video['pafy'].streams video['best'] = video['pafy'].getbest() video['bestaudio'] = video['pafy'].getbestaudio() for playlist in Test.playlists: playlist['fetched'] = pafy.get_playlist(playlist['identifier']) Test.hasrun = True
def tube(): if request.method == 'POST': url = request.form['track'] try: v = pafy.new(url) except: return "Invalid URL" s = v.streams audio = v.audiostreams s_urls = [i.url for i in s] meta = { 'title': v.title, 'duration': v.duration, 'bthumb': v.bigthumb, 'thumb':v.thumb, 'likes':v.likes, 'dislikes': v.dislikes, 'desc': v.description } return render_template('output.html', data={'meta_data': meta,'streams':s,'urls':s_urls, 'a_streams': audio}) else: return render_template('home.html')
def test_pafy_download_invalid_dirname(self): """ Test user specified invalid path. """ vid = pafy.new("DsAn_n6O5Ns", gdata=True) self.assertRaises(IOError, vid.audiostreams[-1].download, "/bad/h/")
def test_pafy_download_to_dir(self): """ Test user specified path. """ vid = pafy.new("DsAn_n6O5Ns", gdata=True) vstream = vid.audiostreams[-1].download("/tmp", meta=True) name = "/tmp/WASTE 2 SECONDS OF YOUR LIFE.m4a" self.assertEqual(12880, os.stat(name).st_size)
def test_pafy_download_to_dir(self): """ Test user specified path. """ vid = pafy.new("DsAn_n6O5Ns", gdata=True) vstream = vid.audiostreams[-1].download("/tmp", meta=True) name = "/tmp/WASTE 2 SECONDS OF YOUR LIFE.ogg" self.assertEqual(22675, os.stat(name).st_size)
def download_video(video_id): logger.info('Inside download_video...') pafy.new(video_id).getbest(preftype='mp4')\ .download(config.VID_PATH, quiet=True, meta=True, callback=download_handler)
# Instantiate classifier and video processor classifier_wrapper = ClassifierWrapper(conf.MODEL_PATH, ref_labels, ref_features) video_processor = VideoProcessor(classifier_wrapper, conf.VIDEO_DIR, args.display_video) # Load the YouTube API key with open(conf.YOUTUBE_API_KEY_PATH) as f: api_key = f.read() while True: try: # Iterate over new videos for video_id, video_title in get_next_new_channel_vid( args.channel_id, api_key, conf.PROCESSED_YOUTUBE_IDS): # Get the exact URL of the video file video = pafy.new(f'https://www.youtube.com/watch?v={video_id}') stream = video.getbest(preftype='mp4') # Create an instance of moviepy video - used to cut out the snippets containing the target identites video = VideoFileClip(stream.url) print(f'Processing video with title: {video_title}') video_processor.process(video, conf.NTH_FRAME, conf.RECORD_IF_IN_M_ANAL) print(f'Going to sleep for {conf.SLEEP_INTERVAL} seconds') sleep(conf.SLEEP_INTERVAL) except KeyboardInterrupt: print('Exiting')
def live_video(): net = cv2.dnn.readNet("yolov3-tiny.weights", "yolov3-tiny.cfg") classes = [] with open("coco.names", "r") as f: classes = [line.strip() for line in f.readlines()] layer_names = net.getLayerNames() outputlayers = [ layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers() ] colors = np.random.uniform(0, 255, size=(len(classes), 3)) # loading image url = str(video_get.get()) if url[-5:] == 'video': cap = cv2.VideoCapture(url) elif url == '0': cap = cv2.VideoCapture(0) elif url == '': cap = cv2.VideoCapture("test.mp4") else: try: video = pafy.new(url) best = video.getbest(preftype="mp4") cap = cv2.VideoCapture() cap.open(best.url) except: print( "An incorrect youtube link or an invalid source might be uploaded! \nInstead watch the prototype." ) cap = cv2.VideoCapture("test.mp4") font = cv2.FONT_HERSHEY_PLAIN starting_time = time.time() frame_id = 0 while True: _, frame = cap.read() height, width, channels = frame.shape # reducing 416 to 320 blob = cv2.dnn.blobFromImage(frame, 0.00392, (320, 320), (0, 0, 0), True, crop=False) net.setInput(blob) outs = net.forward(outputlayers) class_ids = [] confidences = [] boxes = [] for out in outs: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.3: center_x = int(detection[0] * width) center_y = int(detection[1] * height) w = int(detection[2] * width) h = int(detection[3] * height) x = int(center_x - w / 2) y = int(center_y - h / 2) boxes.append([x, y, w, h]) confidences.append(float(confidence)) class_ids.append(class_id) indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.4, 0.6) for i in range(len(boxes)): if i in indexes: x, y, w, h = boxes[i] label = str(classes[class_ids[i]]) confidence = confidences[i] color = colors[class_ids[i]] cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2) cv2.putText(frame, label + " " + str(round(confidence, 2)), (x, y + 30), font, 1, (255, 255, 255), 2) elapsed_time = time.time() - starting_time fps = frame_id / elapsed_time cv2.putText(frame, "FPS:" + str(round(fps, 2)), (10, 50), font, 2, (0, 0, 0), 1) cv2.imshow("Image", frame) if cv2.waitKey(40) == 27: break cv2.destroyAllWindows() cap.release()