我必须从特定框架中播放视频,我可以使用openCV,但是如何播放该特定视频的音频我有从必须开始播放视频的框架,我也只想要相应的音频。
cap = cv2.VideoCapture('tcs.mp4')
fps = cap.get(cv2.CAP_PROP_FPS)
count = 0
success = True
while success:
success,frame = cap.read()
count+=1
ts = count/fps
if t == ts:
print("time stamp of current frame:",count/fps)
print("Press Q to quit")
f_count = count
cap.set(cv2.CAP_PROP_POS_FRAMES, f_count)
# Check if camera opened successfully
if (cap.isOpened()== False):
print("Error opening video file")
# Read until video is completed
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
# Display the resulting frame
cv2.imshow('Frame', frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release
# the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
openCV是用于计算机视觉的库。要播放音频,您将必须使用另一个库(即在C 中,将使用FFMPEG(。
Python中的一种替代方案是基于FFMPEG的FfpyPlayer(https://pypi.org/project/project/ffpyplayer/(。
您可以使用OpenCV处理框架,并在同时播放FfpyPlayer抓取的音频框架时显示帧。
一个更好的选择是处理并将视频文件写入OpenCV中的磁盘,然后使用Python-VLC播放。