Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5067

Beginners • Re: Looping video playlist with button interrupt

$
0
0
this is not perfectly seamless but plays videos in your / Videos folder...

sudo apt install mplayer
sudo apt install python3-opencv

Code:

#!/usr/bin/env python3import subprocessimport osimport signalimport globimport cv2import time# define buttons and led gpiosbutton   = 21end      = 20but_led  = 16# setup GPIOfrom gpiozero import Buttonfrom gpiozero import LEDbut   = Button(button)stop  = Button(end)led   = LED(but_led)led.off()# define player Player = "/usr/bin/mplayer"# setup directoriesHome_Files  = []Home_Files.append(os.getlogin())vid_dir = "/home/" + Home_Files[0]+ "/Videos/"# read Video Filesvids = []vids = glob.glob(vid_dir + "*.mp4")# inital parametersrunning  = 1video_no = 1count    = 0# start loop videop = subprocess.Popen([Player,'-fixed-vo',vids[video_no]],shell=False, preexec_fn=os.setsid)video = cv2.VideoCapture(vids[video_no])frame_rate = video.get(cv2.CAP_PROP_FPS)total_num_frames = video.get(cv2.CAP_PROP_FRAME_COUNT)duration = total_num_frames / frame_ratestart = time.monotonic()# main loopwhile running == 1:    if but.is_pressed:        # stop loop videos, play first video        # wait for loop video to end        if count == 0:            poll = p.poll()        else:            poll = q.poll()        while poll == None:            if count == 0:                poll = p.poll()            else:                poll = q.poll()        led.on()        count = 0        # play first video, vids[0]        p = subprocess.Popen([Player,'-fixed-vo',vids[0]],shell=False, preexec_fn=os.setsid)        poll = p.poll()        while poll == None:            poll = p.poll()        led.off()        video_no = 1        count = 0    # stop button    if stop.is_pressed:        os.killpg(p.pid, signal.SIGTERM)        running = 0        led.off()               # loop next video    if time.monotonic() - start > duration - 1:        video_no += 1        count +=1        if count > 1:            count = 0        if video_no > len(vids) - 1:            video_no = 1        video = cv2.VideoCapture(vids[video_no])        frame_rate = video.get(cv2.CAP_PROP_FPS)        total_num_frames = video.get(cv2.CAP_PROP_FRAME_COUNT)        duration = total_num_frames / frame_rate        if count == 0:            p = subprocess.Popen([Player,'-fixed-vo',vids[video_no]],shell=False, preexec_fn=os.setsid)        else:            q = subprocess.Popen([Player,'-fixed-vo',vids[video_no]],shell=False, preexec_fn=os.setsid)        start = time.monotonic()

Statistics: Posted by gordon77 — Sun Dec 22, 2024 11:46 am



Viewing all articles
Browse latest Browse all 5067

Trending Articles