0

I was wondering if anyone could help me out a bit with this problem I cannot solve. I am using Pafy to search Youtube from a text file that has a song name written in it and that gets a new song every few minutes. I am using Watchdog to watch for file modification and when i first run the script, watchdog catches the file modification and runs the pafy and opencv script, but it won't do the same when the following modification occurs.

#watchdog file change monitoring
class MyHandler(FileSystemEventHandler):
    def on_modified(self, event):
        print ("Received modified event - %s." % event.src_path)
        cv2.destroyAllWindows()

if __name__ == "__main__":
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, path='//PLAYING', recursive=False)
    observer.start()
    try:
        while True:
            #read PLAYING.txt
            PLAYING = open('//PLAYING.txt').readline()
            PLAYING = PLAYING[7:]
            print (PLAYING)
            #search youtube based on NowOnAir.txt
            query_string = urllib.parse.urlencode({"search_query" : PLAYING})
            html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string)
            search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode())
            link = ('http://www.youtube.com/watch?v=' + search_results[0])
            videoPafy = pafy.new(link)
            best = videoPafy.getbestvideo()
            videompv = best.url

            #opencv youtube video output
            video = cv2.VideoCapture(videompv)

            while(video.isOpened()):
                ret, frame = video.read()
                resize = cv2.resize(frame, (1680, 1050))
                gray = cv2.cvtColor(resize, cv2.COLOR_BGR2GRAY)
                result = cv2.addWeighted(image, 0.2, resize, 0.8, 0)
                cv2.namedWindow('frame', 0)
                cv2.resizeWindow('frame', 1680, 1050)
                cv2.imshow('frame', result)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

            time.sleep(1)

    except KeyboardInterrupt:
        observer.stop()
    observer.join()

So, I what I want to happen is, when file gets modified, I want openCV to close the window and open a new one with the new youtube query result.

Any suggestions would be quite welcome, thank You in advance.

Luka mikic
  • 23
  • 1
  • 6
  • I would say that you create a function that gets the video link, and that the video (cv2.VideoCapture part) should be a thread safe global variable.... and when your observer says that it is modified, it calls the function to get the new link a creates a new video out of it, in the next iteration the image will be replaced with the new video image in the same frame.... However you should take care of thread safeness (I think that the observer is in separated thread... maybe i am wrong) and maybe change the while video is open to have a if not opened try to open it again – api55 Dec 06 '17 at 08:54
  • i already tried that, but i got the exactly same behaviour. – Luka mikic Dec 06 '17 at 14:06
  • are you at least getting the "Received modified event"? you should edit the post to see what you tried, maybe there is an error or something? – api55 Dec 06 '17 at 14:09

1 Answers1

0

If the file is just updated once per track change, then you could check the timestamp of the file for modification and use that to trigger your search.

import os.path
import time

last_modified  = time.ctime(os.path.getmtime(file))

while True:
    time.sleep(1)
    if last_modified != time.ctime(os.path.getmtime(file)):
       # search for the track on youtube
       last_modified = time.ctime(os.path.getmtime(file))
stepsal
  • 1
  • 2