python - 如何使用 Python 播放视频?
问题描述
我想创建一个简单的程序来帮助我打开我想要的任何视频,只需写下视频的名称;有没有我可以使用的库。
解决方案
在 python 中处理图像和视频的最常见方法是使用 opencv,它是一个功能强大的库,可让您读取 imgs、视频并显示它们。如果您只想重现视频,可以使用如下代码:
import cv2
videoName = yourVideoPathAndName #'DJI_0209.MP4'
#create a videoCapture Object (this allow to read frames one by one)
video = cv2.VideoCapture(videoName)
#check it's ok
if video.isOpened():
print('Video Succefully opened')
else:
print('Something went wrong check if the video name and path is correct')
#define a scale lvl for visualization
scaleLevel = 3 #it means reduce the size to 2**(scaleLevel-1)
windowName = 'Video Reproducer'
cv2.namedWindow(windowName )
#let's reproduce the video
while True:
ret,frame = video.read() #read a single frame
if not ret: #this mean it could not read the frame
print("Could not read the frame")
cv2.destroyWindow(windowName)
break
reescaled_frame = frame
for i in range(scaleLevel-1):
reescaled_frame = cv2.pyrDown(reescaled_frame)
cv2.imshow(windowName, reescaled_frame )
waitKey = (cv2.waitKey(1) & 0xFF)
if waitKey == ord('q'): #if Q pressed you could do something else with other keypress
print("closing video and exiting")
cv2.destroyWindow(windowName)
video.release()
break
推荐阅读
- bash - bash 代码如何在嵌套在第三个函数中时完全返回
- node.js - TypeError:在云函数中将循环结构转换为 JSON
- javascript - 当我调用 app.post 时,req.user 未定义
- c - 将双向链表保存到 C txt 文件
- python-3.x - Lightfm user_features 中的功能数量不正确
- reactjs - 如何在函数中定义道具
- rest - 泽西岛通用服务
- c# - 如何使用声音播放器从属性中播放多个声音文件
- c# - C# 中的远程服务器无法识别文本框 url 字符串
- java - 如何在 Java 中找到符号 glutPostRedisplay()?