-
Notifications
You must be signed in to change notification settings - Fork 300
/
play.py
53 lines (47 loc) · 1.74 KB
/
play.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import cv2
import numpy as np
import os
from ffpyplayer.player import MediaPlayer
from config import Config
_Config = Config('default.yaml')
VIDEO_SAVE_PATH = _Config.SavePath()['VIDEO_SAVE_PATH']
def PlayVideo(video_path):
video=cv2.VideoCapture(video_path)
frame_num = video.get(cv2.CAP_PROP_FRAME_COUNT) #获取视频总帧数
player = MediaPlayer(video_path)
k=0
for i in range(int(frame_num)):
grabbed, frame=video.read()
audio_frame, val = player.get_frame(force_refresh=False)
#print(audio_frame,val)
if audio_frame is None and k>10:
break
else:
k+=1
if not grabbed:
break
if cv2.waitKey(30) & 0xFF == ord("q"):
break
cv2.imshow("PaddleNews", frame)
if val != 'eof' and audio_frame is not None:
img, t = audio_frame
if audio_frame is not None:
video.release()
#cv2.destroyAllWindows()
if __name__ == "__main__":
LAST_TIMESTAMP = 0 #上一个视频的时间戳
while True:
VideoList = os.listdir(VIDEO_SAVE_PATH)
VideoPlayList = []
for video in VideoList:
if video.endswith(".mp4"):
VideoPlayList.append(int(video.replace(".mp4", "")))
VideoPlayList = np.array(VideoPlayList)
Newest_Video = VideoPlayList.max()
#如果有新的视频,播放最新视频;否则,循环随机播放文件夹里的视频
if Newest_Video != LAST_TIMESTAMP:
PlayVideo(VIDEO_SAVE_PATH + str(Newest_Video) + ".mp4")
LAST_TIMESTAMP = Newest_Video
else:
Random_Video = np.random.choice(VideoPlayList,replace=False)
PlayVideo(VIDEO_SAVE_PATH + str(Random_Video) + ".mp4")