python之moviepy库的安装与使用

目的:因为需要保存一个大大的.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 cv2from 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 = 0for videoDir in dirs:videoName = inputVideoPath + str(videoCount) + "/" + left_right_depth + ".mp4"if os.path.exists(videoName):videoElement = VideoFileClip(videoName)videoList.append(videoElement)videoCount = videoCount + 1concatenateProcessLeft = 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 datafilePathList = []fileDataList = []fileCount = 0filePathes = 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 + 1totalline = 0for fileElement in filePathList:lineNumber = 1fileLength = 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 + 1print(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 = 0outputFolderPath = outputVideoPath + "/" + folderName + "/"folderPathes = os.listdir(inputVideoPath)print(folderPathes)for folderPath in  folderPathes:folerType = inputVideoPath + "/" + str(folderCount) + "/" + folderNameprint("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 + "/" + fileInFolderprint("print totalPath")print(totalPath)if not os.path.exists(outputFolderPath):os.mkdir(outputFolderPath)outputFileName = outputFolderPath + "/" + fileInFoldershutil.copyfile(totalPath, outputFileName)folderCount = folderCount + 1#define the main function,from this function your users functions are called
def main():# combine Bluetooth foldercombineFolders("Bluetooth")# combine wifi foldercombineFolders("wifi")# concatenate video leftvideoconcatenate("left")# # # concatenate video rightvideoconcatenate("right")# # #concatenate video depthvideoconcatenate("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))

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/252422.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

oracle 之 安装后pl/sql登录报ora-12154

这个问题一开始困扰了很久。 查的资料是复制一小段代码到tnsnames.ora中 SID名 (DESCRIPTION (ADDRESS (PROTOCOL TCP)(HOST localhost)(PORT 1522)) (CONNECT_DATA (SERVER DEDICATED) (SERVICE_NAME SID名) ) 注意SID名前面不能有任何其他字符&…

如何避免表单重复提交

客户端方案 禁掉提交按钮。 表单提交后使用Javascript使提交按钮disable。这种方法防止心急的用户多次点击按钮。但有个问题&#xff0c;如果客户端把Javascript给禁止掉&#xff0c;这种方法就无效了。 使用Post/Redirect/Get模式 在提交后执行页面重定向&#xff0c;这就是所…

十六进制转八进制

【问题描述】 问题描述 给定n个十六进制正整数&#xff0c;输出它们对应的八进制数。 输入格式 输入的第一行为一个正整数n &#xff08;1<n<10&#xff09;。 接下来n行&#xff0c;每行一个由0~9、大写字母A~F组成的字符串&#xff0c;表示要转换的十六进制正整数&…

使用iai_kinect2标定kinectV2相机

实验背景&#xff1a;因为需要制作bundlefusion需要的数据集&#xff0c;所以需要使用kinectV2相机获取rgbd图像&#xff0c;年前的时候在我的笔记本上安装了libfreenect2库和iai_kinect2&#xff0c;标定过一次kinecv2相机&#xff0c;然后使用kinectv2相机实时获取的图像实现…

tar只解压tar包中某个文件

如果tar包很大&#xff0c;而只想解压出其中某个文件。方法如下&#xff1a; 只想解压出Redis-1.972.tar 中的Changes文件&#xff0c;来查看有哪些更改。 [rootuplooking]# tar -tf Redis-1.972.tar Redis-1.972…

扎克伯格的中文夜:想要成功就不能放弃

10月23日消息。虽然并不太流畅。昨天马克•扎克伯格依旧用中文与清华经管学院主持人完毕了一场对话&#xff1b;在对话中&#xff0c;这位Facebook创始人兼首席运行官阐述了自己学习中文的原因&#xff1a;想要和太太&#xff08;普里西拉•陈&#xff09;的家人交流&#xff1…

python将ros下bag文件的所有topic解析为csv格式

背景&#xff1a;最近在制作kimera的数据集&#xff0c;尤其是运行semantic模块所需要的bag文件中有很多topic&#xff0c;但是很多不知道topic中装的是什么数据&#xff0c;及其格式&#xff0c;所以我就想着怎么可以将bag中的topic都解析数来&#xff0c;这样就能知道bag中都…

十九. Python基础(19)--异常

十九. Python基础(19)--异常 1 ● 捕获异常 if VS异常处理: if是预防异常出现, 异常处理是处理异常出现 异常处理一般格式: try: <...............> #可能得到异常的语句 except <.......>: #捕获是哪种异常 <...............> #出现异常的处理方…

洛谷1052——过河(DP+状态压缩)

题目描述 在河上有一座独木桥&#xff0c;一只青蛙想沿着独木桥从河的一侧跳到另一侧。在桥上有一些石子&#xff0c;青蛙很讨厌踩在这些石子上。由于桥的长度和青蛙一次跳过的距离都是正整数&#xff0c;我们可以把独木桥上青蛙可能到达的点看成数轴上的一串整点&#xff1a;0…

Tensorflow学习教程------tfrecords数据格式生成与读取

首先是生成tfrecords格式的数据&#xff0c;具体代码如下&#xff1a; #coding:utf-8import os import tensorflow as tf from PIL import Imagecwd os.getcwd() 此处我加载的数据目录如下&#xff1a; bt -- 14018.jpg14019.jpg14020.jpgnbt -- 1_ddd.jpg1_dsdfs.jpg1_dfd.…

ROS获取KinectV2相机的彩色图和深度图并制作bundlefusion需要的数据集

背景&#xff1a; 最近在研究BundleFusion&#xff0c;跑通官方数据集后&#xff0c;就想着制作自己的数据集来运行bundlefusion&#xff0e;KinectV2相机可直接获取的图像的分辨率分为三个HD 1920x1080, QHD: 960X540&#xff0c;SD: 512x424.我选择是中间的分辨率qhd. 录制…

Linux下配置tomcat+apr+native应对高并发

摘要&#xff1a;在慢速网络上Tomcat线程数开到300以上的水平&#xff0c;不配APR&#xff0c;基本上300个线程狠快就会用满&#xff0c;以后的请求就只好等待。但是配上APR之后&#xff0c;Tomcat将以JNI的形式调用Apache HTTP服务器的核心动态链接库来处理文件读取或网络传输…

Firefox 66 将阻止自动播放音频和视频

百度智能云 云生态狂欢季 热门云产品1折起>>> 当我们点击一个链接&#xff0c;或者打开新的浏览器选项卡时&#xff0c;浏览器就开始自动播放视频和声音&#xff0c;这是一件十分烦人的事。Chrome 浏览器早已对这些行为下手了&#xff0c;现在 Firefox 也明确表示要…

Windows 10 关闭Hyper-V

以管理员身份运行命令提示符 关闭 bcdedit /set hypervisorlaunchtype off 启用 bcdedit / set hypervisorlaunchtype auto 禁用DG 转载于:https://www.cnblogs.com/Robbery/p/8397767.html

ROS下获取kinectv2相机的仿照TUM数据集格式的彩色图和深度图

准备工作&#xff1a; &#xff11;&#xff0e; ubuntu16.04上安装iai-kinect2, 2. 运行roslaunch kinect2_bridge kinect2_bridge.launch, 3. 运行 rosrun save_rgbd_from_kinect2 save_rgbd_from_kinect2,开始保存图像&#xff0e; 这个保存kinectV2相机的代码如下&…

Java Web 九大内置对象(一)

在Jsp 中一共定义了九个内置对象&#xff0c;分别为&#xff1a; *request HttpServletRequest; *response HttpServletResponse; *session HttpSession; page This(本jsp页面)&#xff1b; *application ServletCon…

Missing URI template variable 'XXXX' for method parameter of type String

原因&#xff1a;就是spring的controller上的RequestMapping的实参和方法里面的形参名字不一致 方法&#xff1a;改成一样就可。 ps.还能用绑定的方法&#xff0c;不建议&#xff0c;因为太麻烦了 RequestMapping(value "/findUser/{id}",method RequestMethod.GET…

css:text-overflow属性

参考文档:www.w3school.com.cn/cssref/pr_t… text-overflow:ellipsis;( 显示省略符号来代表被修剪的文本。)

Failed to load nodelet ‘/kinect2_bridge` of type `kinect2_bridge/kinect2_bridge_nodelet` to manager

之前在我的电脑上配置了libfreenect2和iai_kinect2&#xff0c;现在需要在工控机上重新安装这两个库&#xff0c;讲kinectV2相机安置在婴儿车上&#xff0c;然后使用我的ros下获取kinectV2相机的彩色图和灰度图的脚本&#xff0c;获取深度图和彩色图。 我成功的安装了libfreen…

object转字符串

1、obj.tostring() obj为空时&#xff0c;抛异常。 2、convert.tostring(obj) obj为空时&#xff0c;返回null&#xff1b; 3、(string)obj obj为空时&#xff0c;返回null&#xff1b;obj不是string类型时&#xff0c;抛异常。 4、obj as string obj为空时&#xff0c;返回nul…