[英]How do I extract the frames from a folder of mp4 videos and transfer them to another folder?
I have a folder full of mp4 clips (over 200).我有一个装满 mp4 剪辑的文件夹(超过 200 个)。 I want to take all those clips, extract their frames and send them to another folder to store all the frames in. This is what I have so far (part of the code) but it's only working when I have one mp4 file in the same folder:
我想拍摄所有这些剪辑,提取它们的帧并将它们发送到另一个文件夹以存储所有帧。这是我目前所拥有的(代码的一部分),但它仅在我有一个 mp4 文件时才有效文件夹:
import cv2 # for capturing videos
import math # for mathematical operations
import matplotlib.pyplot as plt # for plotting the images
import pandas as pd
from keras.preprocessing import image # for preprocessing the images
import numpy as np # for mathematical operations
from keras.utils import np_utils
from skimage.transform import resize # for resizing images
count = 0
videoFile = "sample_vid.mp4"
cap = cv2.VideoCapture(videoFile) # capturing the video from the given path
frameRate = cap.get(5) #frame rate
x=1
while(cap.isOpened()):
frameId = cap.get(1) #current frame number
ret, frame = cap.read()
if (ret != True):
break
if (frameId % math.floor(frameRate) == 0):
filename ="frame%d.jpg" % count;count+=1
cv2.imwrite(filename, frame)
cap.release()
print ("Done!")
Again, i'm having some trouble dealing with the file directories in python and looping it so that it goes through all the files in another folder and send the frames extracted into another folder.同样,我在处理 python 中的文件目录并循环它时遇到了一些麻烦,以便它遍历另一个文件夹中的所有文件并将提取的帧发送到另一个文件夹中。
Use glob
lib to find all mp4
files in your folder.使用
glob
lib 查找文件夹中的所有mp4
文件。 Then run video2frames
method against all videos.然后对所有视频运行
video2frames
方法。
import cv2
import math
import glob
def video2frames(video_file_path):
count = 0
cap = cv2.VideoCapture(video_file_path)
frame_rate = cap.get(5)
while cap.isOpened():
frame_id = cap.get(1)
ret, frame = cap.read()
if not ret:
break
if frame_id % math.floor(frame_rate) == 0:
filename = '{}_frame_{}.jpg'.format(video_file_path, count)
count += 1
cv2.imwrite(filename, frame)
cap.release()
videos = glob.glob('/home/adam/*.mp4')
for i, video in enumerate(videos):
print('{}/{} - {}'.format(i+1, len(videos), video))
video2frames(video)
Tested on two videos.测试了两个视频。 Here is what I've got:
这是我所拥有的:
You can use os.walk
to fetch all mp4 names and iterate over them.您可以使用
os.walk
获取所有 mp4 名称并对其进行迭代。 There are other ways detailed in Find all files in a directory with extension .txt in Python (replace txt with mp4). 在 Python中查找扩展名为 .txt 的目录中的所有文件(用 mp4 替换 txt)中详细介绍了其他方法。
Create some files to find:创建一些文件以查找:
import os
with open("tata.mp4","w") as f: f.write(" ")
with open("tata1.mp4","w") as f: f.write(" ")
with open("tata2.mp4","w") as f: f.write(" ")
os.makedirs("./1/2/3")
with open("./1/subtata.mp4","w") as f: f.write(" ")
with open("./1/2/subtata1.mp4","w") as f: f.write(" ")
with open("./1/2/3/subtata2.mp4","w") as f: f.write(" ")
Find files:查找文件:
startdir = "./"
all_files = []
for root,dirs,files in os.walk(startdir):
for file in files:
if file.endswith(".mp4"):
all_files.append(os.path.join(root,file))
print(all_files)
for f in all_files:
# do your thing
Output:输出:
['./tata2.mp4', './tata1.mp4', './tata.mp4',
'./1/subtata.mp4',
'./1/2/subtata1.mp4',
'./1/2/3/subtata2.mp4']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.