def stream(videoServerUrl, model): session = Livestreamer() session.set_option('http-headers', 'referer=https://bongacams.com/%s' % model) url = 'hlsvariant://https:%s/hls/stream_%s/playlist.m3u8' % ( videoServerUrl, model) streams = session.streams(url) stream = streams['best'] fd = stream.open() now = datetime.datetime.now() filePath = '%s/%s.mp4' % (model, model + now.strftime('%Y-%m-%d-%H-%M')) print(' - Start record stream') if not os.path.exists(model): os.makedirs(model) with open(filePath, 'wb') as f: while True: try: data = fd.read(1024) f.write(data) except: print(' - Error write record into file') f.close() return
def _init_stream(self, oauth, channel): session = Livestreamer() session.set_plugin_option(self.LIVESTREAMER_PLUGIN_TWITCH, self.OAUTH_TOKEN_KEY, oauth) session.set_option(self.RING_BUFFER_SIZE_KEY, self.buffer_size) streams = session.streams(self._generate_stream_url(channel)) return streams.get(self.resolution)
class TestSession(unittest.TestCase): PluginPath = os.path.join(os.path.dirname(__file__), "plugins") def setUp(self): self.session = Livestreamer() self.session.load_plugins(self.PluginPath) def test_exceptions(self): try: self.session.resolve_url("invalid url") self.assertTrue(False) except NoPluginError: self.assertTrue(True) def test_load_plugins(self): plugins = self.session.get_plugins() self.assertTrue(plugins["testplugin"]) def test_builtin_plugins(self): plugins = self.session.get_plugins() self.assertTrue("justintv" in plugins) def test_resolve_url(self): plugins = self.session.get_plugins() channel = self.session.resolve_url("http://test.se/channel") self.assertTrue(isinstance(channel, Plugin)) self.assertTrue(isinstance(channel, plugins["testplugin"])) def test_options(self): self.session.set_option("test_option", "option") self.assertEqual(self.session.get_option("test_option"), "option") self.assertEqual(self.session.get_option("non_existing"), None) self.assertEqual( self.session.get_plugin_option("testplugin", "a_option"), "default") self.session.set_plugin_option("testplugin", "another_option", "test") self.assertEqual( self.session.get_plugin_option("testplugin", "another_option"), "test") self.assertEqual( self.session.get_plugin_option("non_existing", "non_existing"), None) self.assertEqual( self.session.get_plugin_option("testplugin", "non_existing"), None) def test_plugin(self): channel = self.session.resolve_url("http://test.se/channel") streams = channel.get_streams() self.assertTrue("best" in streams) self.assertTrue(streams["best"] is streams["1080p"]) self.assertTrue(isinstance(streams["rtmp"], RTMPStream)) self.assertTrue(isinstance(streams["http"], HTTPStream)) self.assertTrue(isinstance(streams["hls"], HLSStream)) self.assertTrue(isinstance(streams["akamaihd"], AkamaiHDStream))
def test_start(model): model_link_api = 'https://chaturbate.com/api/chatvideocontext/' + model + '/' result = requests.get(model_link_api).json() session = Livestreamer() session.set_option('http-header',"referer=https://www.chaturbate.com/{}/".format(model)) stream = session.streams("hlsvariant://{}".format(result['hls_source'].rsplit('?')[0])) stream = stream['best'] fd = stream.open() with open(model + '.mp4', 'wb') as file: while True: data = fd.read(1024) file.write(data)
def serveFile(self, fURL, sendData): from livestreamer import Livestreamer, StreamError, PluginError, NoPluginError session = Livestreamer() if '|' in fURL: sp = fURL.split('|') fURL = sp[0] headers = dict(urlparse.parse_qsl(sp[1])) session.set_option("http-headers", headers) cookies = dict(urlparse.parse_qsl(sp[2])) session.set_option("http-cookie", cookies) streams = session.streams(fURL) except:
def watch(url): session = Livestreamer() session.set_loglevel('info') session.set_logoutput(sys.stdout) session.set_option('http-headers', 'Client-ID=jzkbprff40iqj646a697cyrvl0zt2m6') streams = session.streams(url) stream = streams['audio_only'] fd = stream.open() # read to nowhere while True: fd.read(1024)
def serveFile(self, fURL, sendData): session = Livestreamer() if '|' in fURL: sp = fURL.split('|') fURL = sp[0] headers = dict(urlparse.parse_qsl(sp[1])) #print headers if 'cdn.sstream.pw' in fURL: fURL = fURL.replace('cdn.sstream.pw', random.choice(s)) headers[ 'Host'] = '6b6473616a6b6c647361646a7361643737353637647361393973616768647368686464732e736974656e6f772e6d65'.decode( 'hex') session.set_option("http-headers", headers) session.set_option("http-ssl-verify", False) session.set_option("hls-segment-threads", 3) try: streams = session.streams(fURL) except: traceback.print_exc(file=sys.stdout) self.send_response(403) self.send_response(200) #print "XBMCLocalProxy: Sending headers..." self.end_headers() if (sendData): print "XBMCLocalProxy: Sending data..." fileout = self.wfile try: stream = streams["best"] print streams try: response = stream.open() buf = 'INIT' while (buf != None and len(buf) > 0): buf = response.read(200 * 1024) fileout.write(buf) fileout.flush() response.close() fileout.close() #print time.asctime(), "Closing connection" except socket.error, e: #print time.asctime(), "Client Closed the connection." try: response.close() fileout.close() except Exception, e: return except Exception, e: traceback.print_exc(file=sys.stdout) response.close() fileout.close()
class TestSession(unittest.TestCase): PluginPath = os.path.join(os.path.dirname(__file__), "plugins") def setUp(self): self.session = Livestreamer() self.session.load_plugins(self.PluginPath) def test_exceptions(self): try: self.session.resolve_url("invalid url") self.assertTrue(False) except NoPluginError: self.assertTrue(True) def test_load_plugins(self): plugins = self.session.get_plugins() self.assertTrue(plugins["testplugin"]) def test_builtin_plugins(self): plugins = self.session.get_plugins() self.assertTrue("justintv" in plugins) def test_resolve_url(self): plugins = self.session.get_plugins() channel = self.session.resolve_url("http://test.se/channel") self.assertTrue(isinstance(channel, Plugin)) self.assertTrue(isinstance(channel, plugins["testplugin"])) def test_options(self): self.session.set_option("test_option", "option") self.assertEqual(self.session.get_option("test_option"), "option") self.assertEqual(self.session.get_option("non_existing"), None) self.assertEqual(self.session.get_plugin_option("testplugin", "a_option"), "default") self.session.set_plugin_option("testplugin", "another_option", "test") self.assertEqual(self.session.get_plugin_option("testplugin", "another_option"), "test") self.assertEqual(self.session.get_plugin_option("non_existing", "non_existing"), None) self.assertEqual(self.session.get_plugin_option("testplugin", "non_existing"), None) def test_plugin(self): channel = self.session.resolve_url("http://test.se/channel") streams = channel.get_streams() self.assertTrue("best" in streams) self.assertTrue(streams["best"] is streams["1080p"]) self.assertTrue(isinstance(streams["rtmp"], RTMPStream)) self.assertTrue(isinstance(streams["http"], HTTPStream)) self.assertTrue(isinstance(streams["hls"], HLSStream)) self.assertTrue(isinstance(streams["akamaihd"], AkamaiHDStream))
def startRecording(model): global postProcessingCommand global processingQueue try: result = requests.get('https://chaturbate.com/api/chatvideocontext/{}/'.format(model)).text result = json.loads(result) session = Livestreamer() session.set_option('http-headers', "referer=https://www.chaturbate.com/{}".format(model)) streams = session.streams("hlsvariant://{}".format(result['hls_source'].rsplit('?')[0])) stream = streams["best"] fd = stream.open() now = datetime.datetime.now() filePath = directory_structure.format(path=save_directory, model=model, gender=result['broadcaster_gender'], seconds=now.strftime("%S"), minutes=now.strftime("%M"), hour=now.strftime("%H"), day=now.strftime("%d"), month=now.strftime("%m"), year=now.strftime("%Y")) directory = filePath.rsplit('/', 1)[0]+'/' if not os.path.exists(directory): os.makedirs(directory) with open(filePath, 'wb') as f: recording.append(model) while True: try: data = fd.read(1024) f.write(data) except: f.close() recording.remove(model) if postProcessingCommand != "": processingQueue.put({'model':model, 'path':filePath, 'gender':gender}) elif completed_directory != "": finishedDir = completed_directory.format(path=save_directory, model=model, gender=gender, seconds=now.strftime("%S"), minutes=now.strftime("%M"), hour=now.strftime("%H"), day=now.strftime("%d"), month=now.strftime("%m"), year=now.strftime("%Y")) if not os.path.exists(finishedDir): os.makedirs(finishedDir) os.rename(filePath, finishedDir+'/'+filePath.rsplit['/',1][0]) return if model in recording: recording.remove(model) except: if model in recording: recording.remove(model)
def serveFile(self, fURL, sendData): session = Livestreamer() if '|' in fURL: sp = fURL.split('|') fURL = sp[0] headers = dict(urlparse.parse_qsl(sp[1])) if 'cdn.sstream.pw' in fURL: fURL = fURL.replace('cdn.sstream.pw',random.choice(s)) headers['Host'] = '6b6473616a6b6c647361646a7361643737353637647361393973616768647368686464732e736974656e6f772e6d65'.decode('hex') session.set_option("http-headers", headers) session.set_option("http-ssl-verify",False) session.set_option("hls-segment-threads",3) try: streams = session.streams(fURL) except: traceback.print_exc(file=sys.stdout) self.send_response(403) self.send_response(200) #print "XBMCLocalProxy: Sending headers..." self.end_headers() if (sendData): #print "XBMCLocalProxy: Sending data..." fileout = self.wfile try: stream = streams["best"] try: response = stream.open() buf = 'INIT' while (buf != None and len(buf) > 0): buf = response.read(200 * 1024) fileout.write(buf) fileout.flush() response.close() fileout.close() #print time.asctime(), "Closing connection" except socket.error, e: #print time.asctime(), "Client Closed the connection." try: response.close() fileout.close() except Exception, e: return except Exception, e: traceback.print_exc(file=sys.stdout) response.close() fileout.close()
def serveFile(self, fURL, sendData): session = Livestreamer() if '|' in fURL: sp = fURL.split('|') fURL = sp[0] headers = dict(urlparse.parse_qsl(sp[1])) print 'LocalProxy: Headers : %s' % headers session.set_option("http-headers", headers) session.set_option("http-ssl-verify", False) session.set_option("hls-segment-threads", 3) try: streams = session.streams(fURL) print "LocalProxy: Streams %s" % streams except: traceback.print_exc() self.send_response(404) self.send_response(200) print "LocalProxy: Sending headers..." self.end_headers() if (sendData): print "LocalProxy: Sending data..." fileout = self.wfile try: stream = streams["best"] try: response = stream.open() buf = 'INIT' while (buf != None and len(buf) > 0): buf = response.read(200 * 1024) fileout.write(buf) fileout.flush() response.close() fileout.close() print time.asctime(), "LocalProxy: Closing connection" except socket.error, e: print time.asctime( ), "LocalProxy: Client Closed the connection." try: response.close() fileout.close() except Exception, e: return except Exception, e: traceback.print_exc() response.close() fileout.close()
def startRecording(model): try: model = model.lower() req = urllib.request.Request('https://www.cam4.com/' + model) req.add_header('UserAgent', UserAgent) resp = urllib.request.urlopen(req) resp = resp.read().decode().splitlines() videoPlayUrl = "" videoAppUrl = "" for line in resp: if "videoPlayUrl" in line: for part in line.split("&"): if "videoPlayUrl" in part and videoPlayUrl == "": videoPlayUrl = part[13:] elif "videoAppUrl" in part and videoAppUrl == "": videoAppUrl = part.split("//")[1] session = Livestreamer() session.set_option('http-headers', "referer=https://www.cam4.com/{}".format(model)) streams = session.streams( "hlsvariant://https://{}/amlst:{}_aac/playlist.m3u8?referer=www.cam4.com×tamp={}" .format(videoAppUrl, videoPlayUrl, str(int(time.time() * 1000)))) stream = streams["best"] fd = stream.open() ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime("%Y.%m.%d_%H.%M.%S") if not os.path.exists("{path}/{model}".format(path=save_directory, model=model)): os.makedirs("{path}/{model}".format(path=save_directory, model=model)) with open( "{path}/{model}/{st}_{model}.mp4".format(path=save_directory, model=model, st=st), 'wb') as f: recording.append(model) while True: try: data = fd.read(1024) f.write(data) except: recording.remove(model) if model in recording: recording.remove(model) except: if model in recording: recording.remove(model)
def startRecording(model): try: URL = "https://chaturbate.com/{}/".format(model) result = urllib.request.urlopen(URL) result = result.read().decode() for line in result.splitlines(): if "m3u8" in line: stream = line.split("'")[1] break soup = BeautifulSoup(result, 'lxml') soup = soup.findAll('div', id="tabs_content_container") for line in str(soup).split(): if 'Sex:' in line: gender = line.split("</dt><dd>")[1][:-5].lower() session = Livestreamer() session.set_option('http-headers', "referer=https://www.cam4.com/{}".format(model)) streams = session.streams("hlsvariant://{}".format(stream)) stream = streams["best"] fd = stream.open() ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime("%d.%m.%Y_%H.%M.%S") if not os.path.exists("{path}/{model}_{gender}".format( path=save_directory, model=model, gender=gender)): os.makedirs("{path}/{model}_{gender}".format(path=save_directory, model=model, gender=gender)) with open( "{path}/{model}_{gender}/{st}_{model}_{gender}.mp4".format( path=save_directory, model=model, gender=gender, st=st), 'wb') as f: recording.append(model) while True: try: data = fd.read(1024) f.write(data) except: f.close() recording.remove(model) return if model in recording: recording.remove(model) except: if model in recording: recording.remove(model)
def get_streams(self): live = Livestreamer() print self.url live.set_option("http-ssl-verify", False) streams = None live.load_plugins(os.path.join(os.getcwd(), "plugins")) try: plugin = live.resolve_url(self.url) streams = plugin.get_streams() self.play_url = stream_to_url(streams.get("best")) except NoPluginError: print("No plugin can handle URL") except PluginError as err: print("{0}", err)
def main(): session = Livestreamer() session.set_option("http-headers", "Client-ID=jzkbprff40iqj646a697cyrvl0zt2m6") streams = session.streams("twitch.tv/tsm_myth") stream = streams['720p60'] fname = "downloading.mpg" vid_file = open(fname, "wb") fd = stream.open() new_bytes = 0 for i in range(0, 8 * 1024): new_bytes = fd.read(2048) vid_file.write(new_bytes) print "Done buffering." startReadingFrames(fname, vid_file, fd)
def start(portNum): global LIVESTREAMER LIVESTREAMER = Livestreamer() LIVESTREAMER.set_option('hls-segment-threads', '3') LIVESTREAMER.set_option('hds-segment-threads', '3') LIVESTREAMER.set_option('stream-segment-threads', '3') global httpd #httpd = ThreadedHTTPServer(('', portNum), StreamHandler) httpd = StoppableHTTPServer(('', portNum), StreamHandler) try: #thread.start_new_thread(httpd.serve, ()) t1 = threading.Thread(target = httpd.serve, args = ()) t1.daemon = True t1.start() print "Livestreamer: Server Starts - {0}:{1}".format("localhost", portNum) except Exception as ex: print ex
def serveFile(self, fURL, sendData): session = Livestreamer() if '|' in fURL: sp = fURL.split('|') fURL = sp[0] headers = dict(urlparse.parse_qsl(sp[1])) session.set_option("http-headers", headers) session.set_option("http-ssl-verify",False) session.set_option("hls-segment-threads",2) try: streams = session.streams(fURL) except: traceback.print_exc(file=sys.stdout) self.send_response(403) self.send_response(200) #print "XBMCLocalProxy: Sending headers..." self.end_headers() if (sendData): #print "XBMCLocalProxy: Sending data..." fileout = self.wfile try: stream = streams["best"] try: response = stream.open() buf = 'INIT' while (buf != None and len(buf) > 0): buf = response.read(200 * 1024) fileout.write(buf) fileout.flush() response.close() fileout.close() #print time.asctime(), "Closing connection" except socket.error, e: #print time.asctime(), "Client Closed the connection." try: response.close() fileout.close() except Exception, e: return except Exception, e: traceback.print_exc(file=sys.stdout) response.close() fileout.close()
def serveFile(self, fURL, sendData): from livestreamer import Livestreamer, StreamError, PluginError, NoPluginError session = Livestreamer() if '|' in fURL: sp = fURL.split('|') fURL = sp[0] headers = dict(urlparse.parse_qsl(sp[1])) session.set_option("http-headers", headers) session.set_option("http-ssl-verify", False) try: streams = session.streams(fURL) except: traceback.print_exc(file=sys.stdout) self.send_response(403) self.send_response(200) #print "XBMCLocalProxy: Sending headers..." self.end_headers() if (sendData): #print "XBMCLocalProxy: Sending data..." fileout = self.wfile try: stream = streams["best"] try: response = stream.open() buf = 'INIT' while (buf != None and len(buf) > 0): buf = response.read(300 * 1024) fileout.write(buf) fileout.flush() response.close() fileout.close() #print time.asctime(), "Closing connection" except socket.error, e: #print time.asctime(), "Client Closed the connection." try: response.close() fileout.close() except Exception, e: return except Exception, e: traceback.print_exc(file=sys.stdout) response.close() fileout.close()
def start(portNum): global LIVESTREAMER LIVESTREAMER = Livestreamer() LIVESTREAMER.set_option('hls-segment-threads', '3') LIVESTREAMER.set_option('hds-segment-threads', '3') LIVESTREAMER.set_option('stream-segment-threads', '3') global httpd #httpd = ThreadedHTTPServer(('', portNum), StreamHandler) httpd = StoppableHTTPServer(('', portNum), StreamHandler) try: #thread.start_new_thread(httpd.serve, ()) t1 = threading.Thread(target = httpd.serve, args = ()) t1.daemon = True t1.start() xbmc.log("Livestreamer: Server Starts - {0}:{1}".format("localhost", portNum), 2) except Exception as ex: xbmc.log("{0}".format(ex), 3)
def startRecording(model): try: URL = "https://chaturbate.com/{}/".format(model) result = urllib.request.urlopen(URL) result = result.read().decode() for line in result.splitlines(): if "m3u8" in line: stream = line.split("'")[1] break session = Livestreamer() session.set_option( 'http-headers', "referer=https://www.chaturbate.com/{}".format(model)) streams = session.streams("hlsvariant://{}".format(stream)) stream = streams["best"] fd = stream.open() ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime("%Y.%m.%d_%H.%M.%S") if not os.path.exists("{path}/{model}".format(path=save_directory, model=model)): os.makedirs("{path}/{model}".format(path=save_directory, model=model)) with open( "{path}/{model}/{st}_{model}.mp4".format(path=save_directory, model=model, st=st), 'wb') as f: recording.append(model) while True: try: data = fd.read(1024) f.write(data) except: f.close() recording.remove(model) return if model in recording: recording.remove(model) except: if model in recording: recording.remove(model)
def serveFile(self, fURL, sendData): session = Livestreamer() if '|' in fURL: sp = fURL.split('|') fURL = sp[0] headers = dict(urlparse.parse_qsl(sp[1])) session.set_option("http-headers", headers) session.set_option("http-ssl-verify", False) try: streams = session.streams(fURL) self.send_response(200) except: self.send_response(403) finally: self.end_headers() if (sendData): with streams["best"].open() as stream: buf = 'INIT' while (len(buf) > 0): buf = stream.read(500 * 1024) self.wfile.write(buf)
def startRecording(model): try: model = model.lower() resp = requests.get('https://www.cam4.com/' + model, headers={'user-agent':'UserAgent'}).text.splitlines() videoPlayUrl = "" videoAppUrl = "" for line in resp: if "videoPlayUrl" in line: for part in line.split("&"): if "videoPlayUrl" in part and videoPlayUrl == "": videoPlayUrl = part[13:] elif "videoAppUrl" in part and videoAppUrl == "": videoAppUrl = part.split("//")[1] session = Livestreamer() session.set_option('http-headers', "referer=https://www.cam4.com/{}".format(model)) streams = session.streams("hlsvariant://https://{}/amlst:{}_aac/playlist.m3u8?referer=www.cam4.com×tamp={}" .format(videoAppUrl, videoPlayUrl, str(int(time.time() * 1000)))) stream = streams["best"] fd = stream.open() ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime("%Y.%m.%d_%H.%M.%S") file = os.path.join(setting['save_directory'], model, "{st}_{model}.mp4".format(path=setting['save_directory'], model=model, st=st)) os.makedirs(os.path.join(setting['save_directory'], model), exist_ok=True) with open(file, 'wb') as f: recording.append(model) while True: try: data = fd.read(1024) f.write(data) except: break if setting['postProcessingCommand']: processingQueue.put({'model': model, 'path': file}) finally: if model in recording: recording.remove(model)
def run(self): print('Starting ' + self.username) session = Livestreamer() session.set_option('http-headers', 'Client-ID=jzkbprff40iqj646a697cyrvl0zt2m6') streams = session.streams("http://twitch.tv/" + self.username) assert len(streams) != 0, 'Stream not open.' directory = os.path.join('images', self.username) try: os.makedirs(directory) except: pass qualities = streams.keys() stream = None if '360p' in qualities: stream = streams['360p'] # elif 'medium' in qualities: # stream = streams['medium'] assert stream is not None, self.username + ': No valid stream quality found.' period = 10 timer = time.time() + period data = b'' with stream.open() as fd: while True: data += fd.read(self.buf_160) if time.time() > timer: timer = time.time() + period ts = str(int(time.time())) fname = self.username + '_' + ts path = os.path.join('movies', fname + '.mp4') print(path) open(path, 'wb').write(data) data = b''
def serveFile(self, fURL, sendData): if (sendData): fURL, quality = player.GetStreamUrl(unquote(fURL)) session = Livestreamer() if '|' in fURL: sp = fURL.split('|') fURL = sp[0] headers = dict(urlparse.parse_qsl(sp[1])) session.set_option("http-headers", headers) session.set_option("http-ssl-verify", False) session.set_option("hls-segment-threads", 3) session.set_option("stream-segment-threads", 3) try: #streams = session.streams(fURL) channel = session.resolve_url(fURL) streams = channel.get_streams() except Exception as ex: traceback.print_exc(file=sys.stdout) self.send_response(403) self.send_response(200) #print "XBMCLocalProxy: Sending headers..." self.end_headers() #print "XBMCLocalProxy: Sending data..." fileout = self.wfile try: stream = streams[quality] try: response = stream.open() buf = 'INIT' while (buf != None and len(buf) > 0): buf = response.read(200 * 1024) fileout.write(buf) fileout.flush() response.close() fileout.close() #print time.asctime(), "Closing connection" except socket.error, e: #print time.asctime(), "Client Closed the connection." try: response.close() fileout.close() except Exception, e: return except Exception, e: traceback.print_exc(file=sys.stdout) response.close() fileout.close()
def dodownload(a): if a.user_id in recording: return recording.append(a.user_id) print(f'\r\033[K{a.user_id}start') live = a.get_streams() if live: playlists = a.host_data['arguments']['playlists'] for i in playlists: if i['mode'] == 0 or i['mode'] == '0': print(f'\r\033[K{a.user_id}获取主列表{i["url"]}') master = i['url'] #session = Streamlink() session = Livestreamer() if threads: session.set_option('hls-segment-threads', int(threads)) if trytimes: session.set_option('hls-segment-attempts', int(trytimes)) session.set_option('hls-live-edge', 9999) session.set_option('hls-segment-timeout', 6.0) session.set_option('hls-timeout', 10.0) session.set_loglevel("none") #cmd = ['streamlink','hls://{}'.format(master),'best','-o','/root/te/t.ts'] #subprocess.call(cmd) streams = session.streams('hlsvariant://' + master) stream = streams["best"] #print(stream.url) error = 0 rstr = r"[\/\\\:\*\?\"\<\>\|\- \n]" oname = a.profile_data['name'] otitle = a.channel_data['title'] name = re.sub(rstr, "_", oname) title = re.sub(rstr, "_", otitle) path = '/root/b/d/fc2/' + str(a.user_id) if not os.path.exists(path): os.makedirs(path) userid = str(a.user_id) while (not error): if a.sameid == 0: break filename = path + '/' + userid + '-' + time.strftime( '%y%m%d_%H%M%S') + '-' + name + '-' + title + '.ts' if len(filename) >= 130: title = '_' filename = path + '/' + userid + '-' + time.strftime( '%y%m%d_%H%M%S') + '-' + name + '-' + title + '.ts' fs = 0 try: ''' cmd = ['ffmpeg','-loglevel','quiet','-y','-i',master,'-c','copy','-fs','1073741824',filename] #cmd = ['ffmpeg','-y','-i',master,'-c','copy','-fs','1073741824',filename,'-loglevel','debug'] error=subprocess.call(cmd) ''' fd = stream.open() f = open(filename, 'wb') desize = 1024 * 1024 * 1024 while 1: ddata = fd.read(8192) if ddata: fs += f.write(ddata) #if fs % 64 == 0: # sys.stdout.write(f'\r\033[K正在录制{len(recording)}{name}{userid}---{round(fs/1024/1024,2)}m') if fs >= desize: fs = 0 f.close() print(f'\r\033[K{filename}文件大小达到限制,切割') shutil.move(filename, '/root/b/d/fc2') filename = path + '/' + userid + '-' + time.strftime( '%y%m%d_%H%M%S' ) + '-' + name + '-' + title + '.ts' f = open(filename, 'wb') else: print(f'{userid}停止录制') break except Exception as e: print(f'\r\033[K{a.user_id}', e) #traceback.print_exc() finally: if 'fd' in locals(): fd.close() if 'f' in locals(): f.close() ff = os.path.getsize(filename) if ff <= 1024 * 100: print(f'\r\033[K{userid}文件下载失败') #cmd = ['ffmpeg','-y','-i',master,'-c','copy','-fs','1073741824',filename,'-loglevel','debug'] #error=subprocess.call(cmd) shutil.move(filename, '/root/b/d/fc2') os.rmdir(path) break #''' print(error) break a.end = True if a.user_id in recording: print(f'\r\033[K{a.user_id}从列表删除') recording.remove(a.user_id) else: print(f'\r\033[K{a.user_id}在列表中找不到{recording}') time.sleep(5) del a
def start_recording(model): global post_processing_command global processing_queue log.info("Start recording : {}".format(model)) try: result = requests.get( 'https://chaturbate.com/api/chatvideocontext/{}/'.format( model)).json() session = Livestreamer() session.set_option( 'http-headers', "referer=https://www.chaturbate.com/{}".format(model)) streams = session.streams("hlsvariant://{}".format( result['hls_source'].rsplit('?')[0])) stream = streams["best"] fd = stream.open() now = datetime.datetime.now() file_path = directory_structure \ .format(path=save_directory, model=model, gender=result['broadcaster_gender'], seconds=now.strftime("%S"), minutes=now.strftime("%M"), hour=now.strftime("%H"), day=now.strftime("%d"), month=now.strftime("%m"), year=now.strftime("%Y")) directory = file_path.rsplit('/', 1)[0] + '/' if not os.path.exists(directory): os.makedirs(directory) log.debug("Creating directory {} for : {}".format( directory, model)) if model in recording: return with open(file_path, 'wb') as f: recording.append(model) log.info("Add to recording : {}".format(model)) while model in wanted: try: data = fd.read(1024) f.write(data) except Exception as e: log.debug("Fail to read stream for : {}".format(model)) log.debug(e) f.close() break if post_processing_command: processing_queue.put({ 'model': model, 'path': file_path, 'gender': gender }) elif completed_directory: finished_dir = completed_directory \ .format(path=save_directory, model=model, gender=gender, seconds=now.strftime("%S"), minutes=now.strftime("%M"), hour=now.strftime("%H"), day=now.strftime("%d"), month=now.strftime("%m"), year=now.strftime("%Y")) if not os.path.exists(finished_dir): os.makedirs(finished_dir) log.debug("Creating directory for : {}".format(model)) os.rename(file_path, finished_dir + '/' + file_path.rsplit['/', 1][0]) log.info("Recording : {}".format(model)) except Exception as e: # log.warning(e) log.debug(e) pass finally: if model in recording: log.info("Stop recording : {}".format(model)) recording.remove(model)
class LivestreamerDumper(object): "Main class for dumping streams" def __init__(self, config_path): """LivestreamerDumper constructor Parameters: config_path: path to user config directory """ self.fd = None self.config_path = config_path def open(self, url, quality): """Attempt to open stream from *url*. Exits with '-1' (using self.exit()) in case of error, including an error msg. """ self.original_url = url try: self.livestreamer = Livestreamer() self._load_config() streams = self.livestreamer.streams(url) except NoPluginError: self.exit("Livestreamer is unable to handle the URL '{}'". format(url)) except PluginError as err: self.exit("Plugin error: {}".format(err)) if quality not in streams: print("Unable to find '{}' stream on URL '{}'" .format(quality, url), file=sys.stderr) self.exit("List of available streams: {}". format(sorted(streams.keys()))) self.stream = streams[quality] try: self.fd = self.stream.open() except StreamError as err: self.exit("Failed to open stream: {}".format(err)) def _load_config(self): "Load and parse config file, pass options to livestreamer" config = SafeConfigParser() config_file = os.path.join(self.config_path, 'settings.ini') config.read(config_file) for option, type in list(AVAILABLE_OPTIONS.items()): if config.has_option('DEFAULT', option): if type == 'int': value = config.getint('DEFAULT', option) if type == 'float': value = config.getfloat('DEFAULT', option) if type == 'bool': value = config.getboolean('DEFAULT', option) if type == 'str': value = config.get('DEFAULT', option) self.livestreamer.set_option(option, value) def get_title(self): """Returns the filename from URL (including extension), that may be: https://www.youtube.com/watch?v=ZEtEH-GIAJE -> '[Hatsune Miku] After Rain Sweet*Drops [English Sub] - YouTube.mp4' https://www.youtube.com/watch?v=ZEtEH-GIAJE -> 'watch_v=ZEtEH-GIAJE.mp4' The former case occurs when URL is a web page with <title> tags. The last case will occur in pages with malformed HTML or when you pass a non-HTML URL as a parameter (for example, a link to a direct HTML5 video). The extension will be detected according to the stream type, for example RTMPStream will always be '.flv'. The only format that may returns a wrong extension is HTTPStream, since there is no standard container in this case. We assume (for now) that every HTTPStream is '.mp4'. """ stream_type = self.stream.__class__.__name__ try: extension = VIDEO_EXTENSIONS[stream_type] except KeyError: print('No extension found...', file=sys.stderr) extension = '' r = requests.get(self.original_url) regex_result = _RE_PAGE_TITLE.search(r.text) if regex_result is not None: filename = regex_result.group(1) # Badly formatted HTML (e.g. no '<title>') else: # 'http://www.example.com/path1/path2?q=V1' -> # 'http', 'www.example.com', '/path1/path2', 'q=V1' split_url = urlsplit(self.original_url) # '/path1/path2' -> 'path2' filename = split_url.path.split('/')[-1] # 'path2' -> 'path2_q=V1' if split_url.query: filename = filename + '_' + split_url.query # Substitute invalid chars for '_' filename = _RE_INVALID_CHARS.sub('_', filename) # Since Windows (Explorer?) has a retarted limit for 255 chars for # filename, including the path, we need to limit the filename to a sane # size. In this case I am using 80 chars. return filename[:80] + extension def stop(self): "If stream is opened, close it" if self.fd: self.fd.close() self.fd = None def exit(self, msg=''): "Close an opened stream and call sys.exit(msg)." self.stop() sys.exit(msg) def dump(self, filepath): "Attempt to dump an opened stream to path *filepath*." common.ask_overwrite(filepath) filename = os.path.basename(filepath) file_size = 0 with open(filepath, 'ab') as f: try: while True: buf = self.fd.read(READ_BUFFER) if not buf: break f.write(buf) file_size = file_size + (READ_BUFFER / KB) print("Downloaded {} KB of file '{}'". format(file_size, filename), end='\r') except KeyboardInterrupt: self.exit("\nPartial download of file '{}'".format(filepath)) print("\nComplete download of file '{}'".format(filepath))
class LivestreamerDumper(object): "Main class for dumping streams" def __init__(self, config_path): """LivestreamerDumper constructor Parameters: config_path: path to user config directory """ self.fd = None self.config_path = config_path def open(self, url, quality): """Attempt to open stream from *url*. Exits with '-1' (using self.exit()) in case of error, including an error msg. """ self.original_url = url try: self.livestreamer = Livestreamer() self._load_config() streams = self.livestreamer.streams(url) except NoPluginError: self.exit( "Livestreamer is unable to handle the URL '{}'".format(url)) except PluginError as err: self.exit("Plugin error: {}".format(err)) if quality not in streams: print("Unable to find '{}' stream on URL '{}'".format( quality, url), file=sys.stderr) self.exit("List of available streams: {}".format( sorted(streams.keys()))) self.stream = streams[quality] try: self.fd = self.stream.open() except StreamError as err: self.exit("Failed to open stream: {}".format(err)) def _load_config(self): "Load and parse config file, pass options to livestreamer" config = SafeConfigParser() config_file = os.path.join(self.config_path, 'settings.ini') config.read(config_file) for option, type in list(AVAILABLE_OPTIONS.items()): if config.has_option('DEFAULT', option): if type == 'int': value = config.getint('DEFAULT', option) if type == 'float': value = config.getfloat('DEFAULT', option) if type == 'bool': value = config.getboolean('DEFAULT', option) if type == 'str': value = config.get('DEFAULT', option) self.livestreamer.set_option(option, value) def get_title(self): """Returns the filename from URL (including extension), that may be: https://www.youtube.com/watch?v=ZEtEH-GIAJE -> '[Hatsune Miku] After Rain Sweet*Drops [English Sub] - YouTube.mp4' https://www.youtube.com/watch?v=ZEtEH-GIAJE -> 'watch_v=ZEtEH-GIAJE.mp4' The former case occurs when URL is a web page with <title> tags. The last case will occur in pages with malformed HTML or when you pass a non-HTML URL as a parameter (for example, a link to a direct HTML5 video). The extension will be detected according to the stream type, for example RTMPStream will always be '.flv'. The only format that may returns a wrong extension is HTTPStream, since there is no standard container in this case. We assume (for now) that every HTTPStream is '.mp4'. """ stream_type = self.stream.__class__.__name__ try: extension = VIDEO_EXTENSIONS[stream_type] except KeyError: print('No extension found...', file=sys.stderr) extension = '' r = requests.get(self.original_url) regex_result = _RE_PAGE_TITLE.search(r.text) if regex_result is not None: filename = regex_result.group(1) # Badly formatted HTML (e.g. no '<title>') else: # 'http://www.example.com/path1/path2?q=V1' -> # 'http', 'www.example.com', '/path1/path2', 'q=V1' split_url = urlsplit(self.original_url) # '/path1/path2' -> 'path2' filename = split_url.path.split('/')[-1] # 'path2' -> 'path2_q=V1' if split_url.query: filename = filename + '_' + split_url.query # Substitute invalid chars for '_' filename = _RE_INVALID_CHARS.sub('_', filename) # Since Windows (Explorer?) has a retarted limit for 255 chars for # filename, including the path, we need to limit the filename to a sane # size. In this case I am using 80 chars. return filename[:80] + extension def stop(self): "If stream is opened, close it" if self.fd: self.fd.close() self.fd = None def exit(self, msg=0): "Close an opened stream and call sys.exit(msg)." self.stop() sys.exit(msg) def dump(self, filepath): "Attempt to dump an opened stream to path *filepath*." common.ask_overwrite(filepath) filename = os.path.basename(filepath) file_size = 0 with open(filepath, 'ab') as f: try: while True: buf = self.fd.read(READ_BUFFER) if not buf: break f.write(buf) file_size = file_size + (READ_BUFFER / KB) print("Downloaded {} KB of file '{}'".format( file_size, filename), end='\r') except KeyboardInterrupt: self.exit("\nPartial download of file '{}'".format(filepath)) print("\nComplete download of file '{}'".format(filepath))
class TestSession(unittest.TestCase): PluginPath = os.path.join(os.path.dirname(__file__), "plugins") def setUp(self): self.session = Livestreamer() self.session.load_plugins(self.PluginPath) def test_exceptions(self): try: self.session.resolve_url("invalid url") self.assertTrue(False) except NoPluginError: self.assertTrue(True) def test_load_plugins(self): plugins = self.session.get_plugins() self.assertTrue(plugins["testplugin"]) def test_builtin_plugins(self): plugins = self.session.get_plugins() self.assertTrue("twitch" in plugins) def test_resolve_url(self): plugins = self.session.get_plugins() channel = self.session.resolve_url("http://test.se/channel") self.assertTrue(isinstance(channel, Plugin)) self.assertTrue(isinstance(channel, plugins["testplugin"])) def test_options(self): self.session.set_option("test_option", "option") self.assertEqual(self.session.get_option("test_option"), "option") self.assertEqual(self.session.get_option("non_existing"), None) self.assertEqual(self.session.get_plugin_option("testplugin", "a_option"), "default") self.session.set_plugin_option("testplugin", "another_option", "test") self.assertEqual(self.session.get_plugin_option("testplugin", "another_option"), "test") self.assertEqual(self.session.get_plugin_option("non_existing", "non_existing"), None) self.assertEqual(self.session.get_plugin_option("testplugin", "non_existing"), None) def test_plugin(self): channel = self.session.resolve_url("http://test.se/channel") streams = channel.get_streams() self.assertTrue("best" in streams) self.assertTrue("worst" in streams) self.assertTrue(streams["best"] is streams["1080p"]) self.assertTrue(streams["worst"] is streams["350k"]) self.assertTrue(isinstance(streams["rtmp"], RTMPStream)) self.assertTrue(isinstance(streams["http"], HTTPStream)) self.assertTrue(isinstance(streams["hls"], HLSStream)) self.assertTrue(isinstance(streams["akamaihd"], AkamaiHDStream)) def test_plugin_stream_types(self): channel = self.session.resolve_url("http://test.se/channel") streams = channel.get_streams(stream_types=["http", "rtmp"]) self.assertTrue(isinstance(streams["480p"], HTTPStream)) self.assertTrue(isinstance(streams["480p_rtmp"], RTMPStream)) streams = channel.get_streams(stream_types=["rtmp", "http"]) self.assertTrue(isinstance(streams["480p"], RTMPStream)) self.assertTrue(isinstance(streams["480p_http"], HTTPStream)) def test_plugin_stream_sorted_excludes(self): channel = self.session.resolve_url("http://test.se/channel") streams = channel.get_streams(sorting_excludes=["1080p", "3000k"]) self.assertTrue("best" in streams) self.assertTrue("worst" in streams) self.assertTrue(streams["best"] is streams["1500k"]) streams = channel.get_streams(sorting_excludes=[">=1080p", ">1500k"]) self.assertTrue(streams["best"] is streams["1500k"]) streams = channel.get_streams(sorting_excludes=lambda q: not q.endswith("p")) self.assertTrue(streams["best"] is streams["3000k"]) def test_plugin_support(self): channel = self.session.resolve_url("http://test.se/channel") streams = channel.get_streams() self.assertTrue("support" in streams) self.assertTrue(isinstance(streams["support"], HTTPStream))
from threading import Thread channel_url = "" proxies_file = "Proxies_txt/good_proxy.txt" processes = [] max_nb_of_threads = 1000 all_proxies = [] nb_of_proxies = 0 # Session creating for request ua = UserAgent() session = Livestreamer() session.set_option("http-headers", { 'User-Agent': ua.random, "Client-ID": "ewvlchtxgqq88ru9gmfp1gmyt6h2b93" }) def print_exception(): exc_type, exc_obj, tb = sys.exc_info() f = tb.tb_frame lineno = tb.tb_lineno filename = f.f_code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) print('EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)) def get_channel():
"manually instead:\n{0}".format(url)) if from_stream: while client_id == "": authenticate_twitch_oauth() print( "Opening browser, copy the client_id from the url and paste it in..." ) client_id = input("Client ID: ") while stream_url == "": stream_url = input("Streamer URL: ") session = Livestreamer() session.set_option("http-headers", {"client-id": client_id}) session.set_option("hls-live-edge", 1) #streams = session.streams("https://www.twitch.tv/xyz") streams = session.streams(stream_url) if len(streams) == 0: print("No streams found on %s" % stream_url) exit() if "720p" in streams: print("Playing '720p' Stream") stream = streams["720p"] elif "1080p" in streams: print("Playing '1080p' Stream (WARNING THIS MIGHT BE LAGGY)") stream = streams["1080p"] else:
def startRecording(model): global postProcessingCommand global processingQueue try: gender = "" URL = "https://chaturbate.com/{}/".format(model) result = requests.get(URL, headers={'Connection': 'close'}) result = result.text for line in result.splitlines(): if "m3u8" in line: stream = line.split("'")[1] break soup = BeautifulSoup(result, 'lxml') soup = soup.find('div', {'id': "tabs_content_container"}) soup = soup.find('dl') for line in str(soup).splitlines(): if "<dt>Sex:</dt>" in line: gender = re.sub("<dt>Sex:</dt><dd>", "", line)[:-5] break session = Livestreamer() session.set_option( 'http-headers', "referer=https://www.chaturbate.com/{}".format(model)) streams = session.streams("hlsvariant://{}".format(stream)) stream = streams["best"] fd = stream.open() now = datetime.datetime.now() filePath = directory_structure.format(path=save_directory, model=model, gender=gender, seconds=now.strftime("%S"), minutes=now.strftime("%M"), hour=now.strftime("%H"), day=now.strftime("%d"), month=now.strftime("%m"), year=now.strftime("%Y")) directory = filePath.rsplit('/', 1)[0] + '/' if not os.path.exists(directory): os.makedirs(directory) with open(filePath, 'wb') as f: recording.append(model) while True: try: data = fd.read(1024) f.write(data) except: f.close() recording.remove(model) if postProcessingCommand != "": processingQueue.put({ 'model': model, 'path': filePath, 'gender': gender }) elif completed_directory != "": finishedDir = completed_directory.format( path=save_directory, model=model, gender=gender, seconds=now.strftime("%S"), minutes=now.strftime("%M"), hour=now.strftime("%H"), day=now.strftime("%d"), month=now.strftime("%m"), year=now.strftime("%Y")) if not os.path.exists(finishedDir): os.makedirs(finishedDir) os.rename( filePath, finishedDir + '/' + filePath.rsplit['/', 1][0]) return if model in recording: recording.remove(model) except: if model in recording: recording.remove(model)
class Recorder: def __init__(self, model, save_directory): self.__model = model self.save_directory = save_directory options_url = 'https://it.chaturbate.com/api/chatvideocontext/{}'.format( model) self.options = json.loads(requests.get(options_url).content) self.session = Livestreamer() self.session.set_option('http-headers', 'referer={}'.format(options_url)) self.streams = None self.__get_live_streams() self.__record_status = True def __get_live_streams(self): hls = self.options.get('hls_source') if hls: self.streams = self.session.streams('hlsvariant://{}'.format(hls)) def __get_stream(self): stream = self.streams['best'] stream_720 = self.streams.get('720p') stream_480 = self.streams.get('480p') if stream_720: return stream_720 elif stream_480: return stream_720 else: return stream def record(self): try: if not self.streams: print('{} No live'.format(self.__model)) return stream = self.__get_stream() fd = stream.open() ts = time() st = datetime.fromtimestamp(ts).strftime( "%Y.%m.%d_%H.%M.%S") # start time if not os.path.exists(self.save_directory): os.makedirs(self.save_directory) with open( "{path}/{st}_{model}.mp4".format(path=self.save_directory, model=self.__model, st=st), 'wb') as f: while self.__record_status: try: data = fd.read(1024) f.write(data) except Exception as exc: print(exc) f.close() return except Exception as exc: print(exc) def stop(self): self.__record_status = False
import cv2 import numpy as np import time from livestreamer import Livestreamer session = Livestreamer() session.set_option("http-headers", "Client-ID=jzkbprff40iqj646a697cyrvl0zt2m6") streams = session.streams("https://www.twitch.tv/___") stream = streams['best'] classificador = cv2.CascadeClassifier("./haarcascade-frontalface-default.xml") fname = "test.mpg" vid_file = open(fname,"wb") fd = stream.open() for i in range(0,2*2048): if i%256==0: print("Buffering...") new_bytes = fd.read(1024) vid_file.write(new_bytes) print("Done buffering.") cam = cv2.VideoCapture(fname) while True: ret, img = cam.read() try: if ret: imagemCinza = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) facesDetectadas = classificador.detectMultiScale(imagemCinza, scaleFactor=1.5, minSize=(100,100))