Skip to content
This repository was archived by the owner on Feb 21, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/drivers/YoutubeServer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
configure_file(
youtubeserver.in
youtubeserver
@ONLY
)
## INSTALL ##

install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/youtubeserver
PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ
DESTINATION bin
COMPONENT youtubeserver-python
)

# Install .py
FILE(GLOB_RECURSE HEADERS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*py)
FOREACH(header ${HEADERS_FILES})
INSTALL(FILES ${header} DESTINATION share/jderobot/python/youtubeserver/ COMPONENT youtubeserver-python)
ENDFOREACH(header)


INSTALL (FILES ${CMAKE_CURRENT_SOURCE_DIR}/youtubeserver.yml DESTINATION ${CMAKE_INSTALL_PREFIX}/share/jderobot/conf COMPONENT youtubeserver-python)
19 changes: 11 additions & 8 deletions src/drivers/YoutubeServer/JdeRobot/ImageProviderI.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ def add(self, job):

class Job(object):

def __init__(self,cb,formato,lock):
def __init__(self,cb,formato,lock, outdir):
self.cb = cb
self.format = formato
self.imageDescription = jderobot.ImageData()
self.lock = lock
self.outdir = outdir

def execute(self):
if not self.getData():
Expand All @@ -39,10 +40,11 @@ def getData(self):

self.imageDescription = jderobot.ImageData()
self.lock.acquire()
self.imageDescription.description = ImageProviderI.getImageDescription(self)
if os.path.isfile('./image.jpg'):
self.imageP = ImageProviderI(self, self.outdir)
self.imageDescription.description = self.imageP.getImageDescription()
if os.path.isfile(self.outdir + 'image.jpg'):
try:
self.im = Image.open('./image.jpg','r')
self.im = Image.open(self.outdir + 'image.jpg','r')
self.lock.release()
self.im = self.im.convert('RGB')
self.imRGB = list(self.im.getdata())
Expand All @@ -64,8 +66,9 @@ def getData(self):

class ImageProviderI(jderobot.Camera):

def __init__(self,workQueue):
def __init__(self,workQueue, outdir):
self.workQueue = workQueue
self.outdir = outdir

def getCameraDescription(self):
return 0
Expand All @@ -85,9 +88,9 @@ def reset(self):
def getImageDescription(self,current=None):

self.imageData = jderobot.ImageDescription()
if os.path.isfile('./image.jpg'):
if os.path.isfile(self.outdir + 'image.jpg'):
try:
self.image= Image.open('./image.jpg','r')
self.image= Image.open(self.outdir + 'image.jpg','r')
self.imageData.width = self.image.width
self.imageData.height = self.image.height
self.image.close()
Expand All @@ -99,5 +102,5 @@ def getImageDescription(self,current=None):
return self.imageData

def getImageData_async(self,cb,formato,curren=None):
job = Job(cb,formato,self.workQueue.lock)
job = Job(cb,formato,self.workQueue.lock, self.outdir)
return self.workQueue.add(job)
Empty file.
10 changes: 0 additions & 10 deletions src/drivers/YoutubeServer/config.yml

This file was deleted.

Empty file.
48 changes: 30 additions & 18 deletions src/drivers/YoutubeServer/videoTools/processVideo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,78 +6,90 @@

class processVideo():

def __init__(self,URL,liveBroadcast):
def __init__(self,URL,liveBroadcast, outdir, fps, ydlFormat):

self.url = URL
self.liveBroadcast = liveBroadcast
self.endDownloading = True
self.endDownloading = False
self.getImages = True
self.outdir = outdir
self.fps = fps
self.fmt = ydlFormat
self.setFileList()

def setFileList(self):

if self.liveBroadcast:
command = shlex.split('youtube-dl -f 92 -g ' + self.url)
else:
command = shlex.split('youtube-dl -f 18 -g ' + self.url)
command = shlex.split('youtube-dl -f '+ self.fmt +' -g ' + self.url)
print command
process= Popen(command,stdout=PIPE,stderr=PIPE)
stdout, sterr = process.communicate()
process.terminate()
if process.poll() is None:
process.terminate()
self.fileList = stdout

def getImage(self,init_time,end_time):

init_time = datetime.strftime(init_time,'%H:%M:%S')
end_time = datetime.strftime(end_time,'%H:%M:%S')
command = shlex.split("ffmpeg -i output.ts -start_number 0 -vf fps=25 -ss " + init_time + " -to " + end_time + " -f image2 -updatefirst 1 temp.jpg")
command = shlex.split("ffmpeg -i "+ self.outdir +"output.ts -start_number 0 -vf fps="+ self.fps + " -ss " + init_time + " -to " + end_time + " -f image2 -updatefirst 1 "+ self.outdir +"temp.jpg")
print command
process= Popen(command,stdout=PIPE,stderr=PIPE)
code = process.poll()
while (code == None):
code = process.poll()
process.terminate()
if process.poll() is None:
print 'a'
process.terminate()

def downloadVideo(self):
try:
if os.path.isfile('output.ts'):
os.remove("./output.ts")
if os.path.isfile(self.outdir + 'output.ts'):
os.remove(self.outdir + "output.ts")

data = self.fileList.splitlines()
data= data[0].decode('utf-8')
if self.liveBroadcast:
command=shlex.split('ffmpeg -i ' + data + ' -c copy output.ts')
command=shlex.split('ffmpeg -i ' + data + ' -c copy ' + self.outdir +'output.ts')
else:
command=shlex.split('ffmpeg -r 24 -i ' + data + ' -r 24 output.ts')
command=shlex.split('ffmpeg -r '+ self.fps +' -i ' + data + ' -r '+ self.fps +' ' + self.outdir + 'output.ts')
print command
process= Popen(command,stdout=PIPE,stderr=PIPE)
code = process.poll()
while (code == None):
code = process.poll()

print "Download finish"
print "*** Download finished ***"
self.endDownloading = True
except:
print "Can't download video"
print sys.exc_info[0]
finally:
process.terminate()
if process.poll() is None:
process.terminate()

def changeName(self):
if os.path.isfile('./temp.jpg'):
os.rename("temp.jpg","image.jpg")
if os.path.isfile(self.outdir + 'temp.jpg'):
os.rename(self.outdir + "temp.jpg",self.outdir + "image.jpg")

def getVideoDuration(self):
try:
if os.path.isfile('output.ts'):
command = shlex.split('ffprobe -show_entries format=duration -sexagesimal output.ts')
if os.path.isfile(self.outdir + 'output.ts'):
command = shlex.split('ffprobe -show_entries format=duration -sexagesimal '+ self.outdir + 'output.ts')
print command
process = Popen(command ,stdout=PIPE,stderr=PIPE)
time,stderr = process.communicate()
time = time.decode('utf-8')
time = time.split('\n')[1].split('=')[1]
time = datetime.strptime(time.split('.')[0],'%H:%M:%S')

else:
time = datetime.strptime('00:00:01', '%H:%M:%S')
except:
time = datetime.strptime('00:00:01', '%H:%M:%S')
finally:
process.terminate()

if process.poll() is None:
process.terminate()
return time
14 changes: 8 additions & 6 deletions src/drivers/YoutubeServer/videoTools/threadFlow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@
import os
from datetime import timedelta,datetime


class ThreadImage(threading.Thread):

def __init__(self,dataFlow,liveBroadcast):
def __init__(self,dataFlow,liveBroadcast, outdir):
self.dataFlow = dataFlow
self.format_time = '%H:%M:%S'
self.init_time = datetime.strptime('00:00:00',self.format_time)
self.liveBroadcast = liveBroadcast
self.outdir = outdir
threading.Thread.__init__(self)

def run(self):
print "Getting Images"
print "Getting Images..."
_run = True
while not os.path.isfile('./output.ts'):
while not os.path.isfile(self.outdir + 'output.ts'):
time.sleep(1)

while(_run == True):
self.end_time = self.dataFlow.getVideoDuration()

if (self.end_time == self.init_time) and (self.dataFlow.endDownloading):
_run = False
self.dataFlow.getImage(self.init_time,self.end_time)
if not(self.end_time == self.init_time):
self.dataFlow.getImage(self.init_time,self.end_time)
self.init_time = self.end_time

class ThreadDownload(threading.Thread):
Expand All @@ -32,7 +34,7 @@ def __init__(self,dataFlow):
threading.Thread.__init__(self)

def run(self):
print("Download Started")
print("*** Download Started ***")
self.dataFlow.downloadVideo()

class ThreadChangeName(threading.Thread):
Expand Down
3 changes: 3 additions & 0 deletions src/drivers/YoutubeServer/youtubeserver.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

python @CMAKE_INSTALL_PREFIX@/share/jderobot/python/youtubeserver/youtubeserver.py $*
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
import comm
import Ice
import threading
import os
from PIL import Image
from easyiceconfig import easyiceconfig as EasyIce
from videoTools.processVideo import processVideo
from videoTools.threadFlow import ThreadImage,ThreadDownload,ThreadChangeName
from JdeRobot.ImageProviderI import ImageProviderI,WorkQueue


def cleanup():

if os.path.isfile( outdir + 'output.ts'):
os.remove(outdir + "output.ts")
if os.path.isfile(outdir + 'temp.jpg'):
os.remove(outdir + "temp.jpg")
if os.path.isfile(outdir + 'image.jpg'):
os.remove(outdir + "image.jpg")


if __name__== "__main__":

Expand All @@ -23,27 +33,43 @@
URL = cfg.getProperty("youtubeServer.ImageSrv.URL")
liveBroadcast = cfg.getProperty("youtubeServer.ImageSrv.LiveBroadcast")
print(endpoint)
outdir = cfg.getProperty("youtubeServer.ImageSrv.OutputDir")
fps = cfg.getProperty("youtubeServer.ImageSrv.FPS")
ydlFormat = cfg.getProperty("youtubeServer.ImageSrv.Format")

if type(fps) is not str:
fps = str(fps)
if type(ydlFormat) is not str:
ydlFormat = str(ydlFormat)


lock = threading.Lock()
workQueue = WorkQueue(lock)
workQueue.setDaemon(True)

dataFlow = processVideo(URL,liveBroadcast)
dataFlow = processVideo(URL,liveBroadcast, outdir, fps, ydlFormat)

downloadThread = ThreadDownload(dataFlow)
imageThread = ThreadImage(dataFlow,liveBroadcast)
imageThread = ThreadImage(dataFlow,liveBroadcast, outdir)
nameThread = ThreadChangeName(dataFlow,lock)

downloadThread.daemon = True
imageThread.daemon = True
nameThread.daemon = True
workQueue.daemon = True

downloadThread.start()
imageThread.start()
nameThread.start()
workQueue.start()

adapter = ic.createObjectAdapterWithEndpoints("youtubeServer",endpoint) #Properties
object = ImageProviderI(workQueue)
object = ImageProviderI(workQueue, outdir)
adapter.add(object, Ice.stringToIdentity("youtubeServer"))
adapter.activate()
workQueue.join()
print('esperar a cierre')
ic.waitForShutdown()
except KeyboardInterrupt:
sys.exit()
print 'Cleaning up & killing...'
cleanup()
sys.exit(0)
13 changes: 13 additions & 0 deletions src/drivers/YoutubeServer/youtubeserver.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
youtubeServer:
ImageSrv:
Server: 1 # 0 -> Deactivate, 1 -> Ice , 2 -> ROS (Not supported)
Proxy: "default -h 0.0.0.0 -p 9999"
URL: "https://www.youtube.com/watch?v=zw47_q9wbBE"
LiveBroadcast: False #True for youtube live events
OutputDir: "/tmp/" #where do you want to store temporal files. INCLUDE THE FINAL /
FPS: 24 #framerate to serve images
Format: 18 #youtube-dl format to NOT streaming video download. Run: 'youtube-dl --list-formats [URL]' to see available video formats
Name: youtubeServer_py

NodeName: youtubeServerCfg