【问题标题】:How do I extract the frames from a folder of mp4 videos and transfer them to another folder?如何从 mp4 视频文件夹中提取帧并将它们传输到另一个文件夹?
【发布时间】:2019-07-24 12:59:38
【问题描述】:

我有一个装满 mp4 剪辑的文件夹(超过 200 个)。我想获取所有这些剪辑,提取它们的帧并将它们发送到另一个文件夹以存储所有帧。这是我到目前为止所拥有的(部分代码)但它只有在我有一个 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!")

再次,我在处理 python 中的文件目录和循环它时遇到了一些麻烦,以便它遍历另一个文件夹中的所有文件并将提取的帧发送到另一个文件夹。

【问题讨论】:

  • 你有什么错误?
  • 我不知道如何在我的代码中使用不同的目录。对于同一目录中的单个文件,该功能是可靠的,我只是想获取它,以便它遍历所有文件并将它们传输到一个文件夹。
  • 所以你需要遍历所有文件夹
  • 不,只有一个包含所有 mp4 文件的文件夹。我需要从该 mp4 文件文件夹中提取帧并将它们发送到另一个文件夹

标签: python python-3.x opencv scikit-image cv2


【解决方案1】:

使用glob lib 查找文件夹中的所有mp4 文件。然后对所有视频运行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)

在两个视频上进行了测试。这是我所拥有的:

【讨论】:

  • video2frames(video) 中的视频目录在哪里?那是指帧的去向吗?那只是“用户/文档/文件夹”吗? print('{}/{} - {}'.format(i+1, len(videos), video)) 视频和视频有什么区别,我可以看到视频是带有mp4的文件夹的路径,但是什么是视频?
  • videos 是所有视频的列表。 video 是单个视频的路径。所以你将video 传递给video2frames 方法,例如:video2frames(c:/videos/video.mp4)
  • 好的,但是帧被发送到哪里?
  • 到与视频相同的文件夹
  • 当我运行它时它不会在文件夹中做任何事情。
【解决方案2】:

您可以use os.walk to fetch all mp4 names 并对其进行迭代。 Find all files in a directory with extension .txt in Python中详述了其他方式(把txt换成mp4)。

创建一些文件来查找:

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(" ")

查找文件:

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

输出:

['./tata2.mp4', './tata1.mp4', './tata.mp4', 
 './1/subtata.mp4', 
 './1/2/subtata1.mp4', 
 './1/2/3/subtata2.mp4']

【讨论】:

    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    相关资源
    最近更新 更多