我使用python imageio
包将.mp4
视频转换为.avi
视频,保持fps和大小相同。以下是我的代码:
import imageio
src_dir = "my/source/video.mp4"
dst_dir = "my/dst/video.avi"
reader = imageio.get_reader(src_dir)
fps = reader.get_meta_data()['fps']
writer = imageio.get_writer(dst_dir, fps=fps)
for im in reader:
writer.append_data(im[:, :, :])
writer.close()
我成功了。
但是,我发现video.mp4
为27.1 MB,而video.avi
仅为3.70 MB。
然后我使用cv2
做同样的事情:
import cv2
src_dir = "my/source/video.mp4"
dst_dir = "my/dst/video_1.avi"
video_cap = cv2.VideoCapture(src_dir)
fps = video_cap.get(cv2.cv.CV_CAP_PROP_FPS)
size = (int(video_cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
int(video_cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
video_writer = cv2.VideoWriter(dst_dir, cv2.cv.CV_FOURCC('M', 'J', 'P', 'G'), fps, size)
success, frame = video_cap.read()
while success:
video_writer.write(frame)
success, frame = video_cap.read()
这次我得到video_1.avi
,即65.6 MB
我的问题:
video.avi
(使用imageio
方法)为何如此之小; video.avi
(使用imageio
方法)训练3D-CNN而不是使用video_1.avi
(使用cv2
方法)是否有任何问题? 补充
以下是我的视频文件的信息:
lichenyang@lichenyang-All-Series:~/chalearn2014/script$ file video.mp4
video.mp4: ISO Media, MPEG v4 system, version 2
lichenyang@lichenyang-All-Series:~/chalearn2014/script$ file video.avi
video.avi: RIFF (little-endian) data, AVI, 640 x 480, 20.00 fps, video: H.264 X.264 or H.264
lichenyang@lichenyang-All-Series:~/chalearn2014/script$ file video_1.avi
video_1.avi: RIFF (little-endian) data, AVI, 640 x 480, 20.00 fps, video: Motion JPEG
答案 0 :(得分:0)
我测试了两种方法,发现两种方法都缺少所有声音。
我通过文件命令找到差异:
video.avi:RIFF(小端)数据,AVI,480 x 368,25.00 fps,视频:H.264 X.264或H.264
video_1.avi:RIFF(小端)数据,AVI,480 x 360,25.00 fps,视频:Motion JPEG
我认为所有avi视频都足够好。我没有看到任何问题。您可以尝试并向我们分享更多信息。
顺便说一下,由于Opencv3.2:
,我需要更改第二个代码#!/usr/bin/env python
# encoding: utf-8
import cv2
src_dir = "1.mp4"
dst_dir = "2.avi"
video_cap = cv2.VideoCapture(src_dir)
#fps = video_cap.get(cv2.cv.CV_CAP_PROP_FPS)
#cv2.cv會出現錯誤:AttributeError: 'module' object has no attribute 'cv'
#在Opencv3.2中cv2.CV_CAP_PROP_FPS需要改為cv2.CAP_PROP_FPS
#也就是都要去掉CV_字眼
fps = video_cap.get(cv2.CAP_PROP_FPS)
size = (int(video_cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
#video_writer = cv2.VideoWriter_fourcc(dst_dir, cv2.FOURCC('M', 'J', 'P', 'G'), fps, size)
video_writer = cv2.VideoWriter(dst_dir, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, size)
success, frame = video_cap.read()
while success:
video_writer.write(frame)
success, frame = video_cap.read()