输入视频路径获取视频长宽python Image
时间: 2024-10-16 07:15:39 浏览: 41
在Python中,我们可以使用`imageio`库读取视频帧,并结合`PIL`(Pillow)库来处理每一帧以获取视频的宽度和高度。首先确保安装了这两个库,如果没有可以使用以下命令安装:
```bash
pip install imageio pillow
```
然后你可以编写这样的代码片段来获取视频的尺寸:
```python
from imageio import imread
from PIL import Image
def get_video_size(video_path):
try:
# 使用imageio读取视频的第一帧
frame = imread(video_path)
# 使用PIL处理帧并获取尺寸
width, height = Image.fromarray(frame).size
return width, height
except Exception as e:
print(f"Error occurred while reading the video: {e}")
return None
# 调用函数,传入视频路径
video_width, video_height = get_video_size("path_to_your_video.mp4")
if video_width and video_height:
print(f"Video dimensions: Width - {video_width}, Height - {video_height}")
else:
print("Failed to determine video dimensions.")
阅读全文
相关推荐










