python之moviepy库的安装与使用

2023-05-16

目的:因为需要保存一个大大的.mp4视频,以防过程中设备出现异常导致整个长长的视频无法正常保存,所以采用分段保存视频的方式,每500帧保存一段,然后再将视频合到一起.最近刚开始学习python,发现python真的很好用,所以这次就使用python中的moviepy库来完成视频的合并.

一.安装moviepy

1. 你首先尝试使用 pip install moviepy指令是否可以正常安装moviepy库(我在python2.7上和python3.7上都尝试了这中安装方式都安装不了,所以不得不采用下面这个方式)

2.采用source文件安装.参照 https://blog.csdn.net/ucsheep/article/details/81000982 下载这个库的source文件,然后按照目录下的 README.rst 的指示安装,

首先cd到你下载的目录文件下,顺序执行 

$ (sudo) pip install ez_setup
$ (sudo) python setup.py install

 

如果有错误提示

ERROR: moviepy 1.0.3 has requirement imageio<2.5,>=2.0, but you'll have imageio 2.8.0 which is incompatible.

那么就执行

pip install imageio

二.使用moviepy库合并多个视频

我的目录框架是这样的,

 

上面的每个文件目录下的文件是这样的.

 Bluetooth文件目录

 wifi文件目录

 首先是将目录下的所有视频都合并到一起

下面的代码实现视频的合并,文件的合并,多个文件夹下文件的归并

# -*-coding:utf-8-*-
# this python script is to concatenate a sequence of videos into one


# import cv2

from moviepy.editor import *
import os
import linecache
import shutil

#inputVideoPath = "/media/yunlei/Seagate/collection_device_data/20200603/大仟里L2主干道大圈-1591150453197/"
# outputVideoPath = "/media/yunlei/Seagate/collection_device_data/20200603/concatenated/大仟里L2主干道大圈-1591150453197/"
inputVideoPath = "/media/yunlei/Seagate/collection_device_data/20200603/大仟里L2主干道-1591155992285/"

outputVideoPath = "/media/yunlei/Seagate/collection_device_data/20200603/concatenated/大仟里L2主干道-1591155992285/"


def videoconcatenate(left_right_depth):
    print("this function is to implement concatenation")
    dirs = os.listdir(inputVideoPath)
    videoList = []
    videoCount = 0
    for videoDir in dirs:
        videoName = inputVideoPath + str(videoCount) + "/" + left_right_depth + ".mp4"
        if os.path.exists(videoName):
            videoElement = VideoFileClip(videoName)
            videoList.append(videoElement)
        videoCount = videoCount + 1
    concatenateProcessLeft = concatenate_videoclips(videoList)
    concatenateProcessLeft.to_videofile(outputVideoPath + "/" + left_right_depth + ".mp4", fps = 20,  remove_temp = False)

def combinefiles(fileName):
    print("start to comebine files")
    #This list is used to store all file data
    filePathList = []
    fileDataList = []

    fileCount = 0

    filePathes = os.listdir(inputVideoPath)
    for filePath in filePathes:
        fileType = inputVideoPath + str(fileCount) + "/" + fileName + ".txt"
        if os.path.exists(fileType):
            filePathList.append(fileType)
            # print(fileType)
        fileCount = fileCount + 1

    totalline = 0
    for fileElement in filePathList:
        lineNumber = 1
        fileLength = len(open(fileElement, encoding='utf-8').readlines())
        totalline = totalline + fileLength
        #print(fileLength)

        while lineNumber <= fileLength:
            line = linecache.getline(fileElement, lineNumber)

            #print(line)
            line = line.strip()
            fileDataList.append(line)
            lineNumber = lineNumber + 1
    print(totalline)

    fileAll = open(outputVideoPath + "/" + fileName + ".txt", 'w+', encoding='utf-8')
    for i, p in enumerate(fileDataList):
        print(i,p)
        fileAll.write(p+'\n')
    fileAll.close()

def combineFolders(folderName):
    folderList = []
    folderCount = 0
    outputFolderPath = outputVideoPath + "/" + folderName + "/"
    folderPathes = os.listdir(inputVideoPath)
    print(folderPathes)
    for folderPath in  folderPathes:
        folerType = inputVideoPath + "/" + str(folderCount) + "/" + folderName
        print("folderType")
        print(folerType)
        print("start to copy file")

        if os.path.exists(folerType):
            filesInFolder  = os.listdir(folerType)
            print("filesInFolder")
            print(filesInFolder)
            for fileInFolder in  filesInFolder:
                totalPath = folerType + "/" + fileInFolder
                print("print totalPath")
                print(totalPath)
                if not os.path.exists(outputFolderPath):
                    os.mkdir(outputFolderPath)
                outputFileName = outputFolderPath + "/" + fileInFolder
                shutil.copyfile(totalPath, outputFileName)
        folderCount = folderCount + 1




#define the main function,from this function your users functions are called
def main():
    # combine Bluetooth folder
    combineFolders("Bluetooth")
    # combine wifi folder
    combineFolders("wifi")
    # concatenate video left
    videoconcatenate("left")
    # # # concatenate video right
    videoconcatenate("right")
    # # #concatenate video depth
    videoconcatenate("depth")
    # combinefiles("video_time")
    combinefiles("Camera_time")
    # combinefiles("Bluetooth_times")
    combinefiles("wifi_times")
#the entrance of this projrct
if __name__ == "__main__":
    main()


































 

因为是刚学习python所以很多时候并不知哪个用法更合适,所以那就尝试一下,比如下面这两个遍历路径下的文件的方式,

for videoDir in dirs:

这种,会将dirs路径下的所有文件都获取到,如果比如说我这里的路径下就包括了文件加和文件,而我希望对文件夹做处理,所以我就要先将文件夹挑拣出来.下面就是我只检索那些是文件夹,并且文件夹上有.mp4格式视频的文件目录我才把他们count in.

    videoLeft = inputVideoPath + str(videoCount) + "/" + "left.mp4"
    videoRight = inputVideoPath + str(videoCount) + "/" + "right.mp4"
    videoDepth = inputVideoPath + str(videoCount) + "/" + "depth.mp4"
    if os.path.exists(videoLeft):

第二种,这种os.walk(path)的方式可以返回root就是根目录path,dirs就是root目录下所有的文件夹,以及文件夹下的文件夹,files就是root path下所有的文件.所以你需要根据你的需求来选择使用哪种遍历方式.

for root, dirs, files in os.walk(inputVideoPath):
	for name in files:
		print(os.path.join(root, name))
	for name in dirs:
		print(os.path.join(root, name))
print(len(videoLeftAll))

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python之moviepy库的安装与使用 的相关文章

随机推荐

  • 适用于搭建内部培训平台的系统,开源了!

    本项目系统是是一款适用于搭建内部培训平台的开源系统 xff0c 企业培训方案 xff0c 致力于为更多企业机构搭建私有化内部培训平台 基于 Java 43 MySQL 开发 xff0c 采用前后端分离模式 xff0c 前台采用 React1
  • 马斯克官宣:卸任推特CEO,继承者是她,网友:不会是女版的你吧!

    整理 朱珂欣 出品 CSDN程序人生 xff08 ID xff1a coder life xff09 推特公司 CEO 马斯克周四在推特上宣布 xff1a 为 X Twitter 公司聘请到了一位新 CEO xff0c 她将在大约 6 周后
  • 一款支持聚合支付,拿来就能用的支付系统,开源了!

    项目介绍 Jeepay是一套适合互联网企业使用的开源支付系统 xff0c 支持多渠道服务商和普通商户模式 已对接微信支付 xff0c 支付宝 xff0c 云闪付官方接口 xff0c 支持聚合码支付 Jeepay使用Spring Boot和A
  • 星标1.4k,一款国产开源数据同步中间件,支持多种数据源和预警功能!

    DBSyncer是一款开源的数据同步中间件 xff0c 提供MySQL Oracle SqlServer PostgreSQL Elasticsearch ES Kafka File SQL等同步场景 支持上传插件自定义同步转换业务 xff
  • 快速构建,一款轻量级的企业知识分享与团队协同软件开源了!

    今天给大家分享一款开源的轻量级的企业知识软件 xff0c 主打一个实用 xff01 介绍 MM Wiki 是一个轻量级的企业知识分享与团队协同软件 xff0c 可用于快速构建企业 Wiki 和团队知识分享平台 部署方便 xff0c 使用简单
  • “sudo: aptitude: command not found”问题解决办法

    caoxuepeng 64 caoxuepeng G3 3579 sudo aptitude install libhdf5 serial dev sudo aptitude command not found 问题描述 xff1a 命令不
  • apm软件仿真+QGC地面站 环境搭建

    本教程使用场景 xff1a apm软件仿真运行于虚拟机 ubuntu环境 qgc地面站位于windows 系统 两个环境在同一台电脑上 一 apm仿真环境搭建 1 安装vm虚拟机及ubuntu 18 2 ubuntu下 下载Ardupilo
  • GPS基础知识(四)、GPS信号结构

    GPS信号结构 xff08 三层 xff09 载波伪码数据码 一 xff0c 载波 载波是三层的基础 xff0c 伪码和数据码都是调制在载波上才能发送 GPS有两个载波频率 xff0c L1和L2 xff0c L1为1575 42MHz x
  • 简述四种干扰观测器(三)————基于扩张状态观测器的干扰观测器

    本节以最广泛应用的二阶系统为例介绍通用的线性观测器 非线性的扩张状态观测器将在将在自抗扰控制中介绍 观测器设计问题 xff0c 就是重新构造一个系统 xff0c 利用原系统中可以直接测量到的输出向量和输入向量作为它的输入信号 xff0c 并
  • Typora+PicGo+阿里云oss

    阿里云Oss图床 43 PicGo 43 Typora 之前用的gitee网上说可能会不稳定 xff0c 我就改成了阿里云OSS对象存储 参考 xff1a PicGo配置阿里云OSS 栗筝i的博客 CSDN博客 picgo 阿里云 1 注册
  • centos安装软件失败

    错误 xff1a 为仓库 appstream 下载元数据失败 Cannot prepare internal mirrorlist No URLs in mirrorlist 问题参考 CentOS Linux 8 AppStream 错误
  • Docker容器安装ssh

    Docker 容器里安装ssh和连接ssh 在服务器创建容器中安装了anaconda xff0c 为了方便敲代码 xff0c 用pycharm连接容器中的anaconda xff0c 我们需要安装ssh服务 前提 创建好docker容器 x
  • 阿里云创始人王坚正式回归阿里云

    5 月 11 日消息 xff0c 阿里云创始人王坚正式回归阿里云 据多名知情人士透露 xff0c 早在 2022 年底 xff0c 王坚其实就已经应张勇等人邀请 xff0c 以幕后顾问的角色进行考察和指导阿里云的工作 经过几个月的磨合和协商
  • random.seed()的用法

    random random 可以用来生成一个随机数 xff0c 如果在生成随机数之前 xff0c 先调用random seed x xff0c x可以是一个随机整数 xff0c 这时候在调用random random xff0c 则种子x和
  • PRGC: Potential Relation and Global Correspondence Based Joint Relational Triple Extraction

    标题 xff1a PRGC xff1a 基于潜在关系和全局对应的联合关系三元组抽取 摘要 联合抽取实体和关系的局限性 xff1a 关系预测的冗余性 xff0c 基于span抽取泛化能力差和效率低 本文从新角度将此任务分解为三个子任务 xff
  • 查看显存使用情况:nvidia-smi

    在装有nvidia驱动的机器终端输入nvidia smi xff0c 可以查看显卡的状况 xff0c 其输出界面如下 xff1a NVIDIA 系统管理接口 xff08 nvidia smi xff09 是一个命令行实用程序 xff0c 基
  • Docker更换Docker Root Dir目录

    原因 xff1a 我在加载自己的镜像时 xff0c 发现root空间不足 xff0c 无法加载 xff0c 所以需要更改Docker Root Dir目录 xff0c 才能放下我的镜像文件 具体方法 一 查看默认目录 执行docker in
  • PHP关于时间的整理

    year 61 date 34 Y 34 month 61 date 34 m 34 day 61 date 34 d 34 dayBegin 61 mktime 0 0 0 month day year 当天开始时间戳 dayEnd 61
  • 制作.sens数据集跑通bundlefusion

    1 主要参考这篇博客实现 https blog csdn net Wuzebiao2016 article details 94426905 2 首先就是将自己采集的RGBD图像的保存格式向Bundlefusion需要的格式对齐 xff0c
  • python之moviepy库的安装与使用

    目的 xff1a 因为需要保存一个大大的 mp4视频 xff0c 以防过程中设备出现异常导致整个长长的视频无法正常保存 xff0c 所以采用分段保存视频的方式 xff0c 每500帧保存一段 xff0c 然后再将视频合到一起 xff0e 最