0. 前言 前段时间购买一台小米的摄像机——小白智能摄像机云台版,价格不贵,但是有个最大的问题就是他以分钟为单位存储视频文件,每分钟的视频都会存储为一个mp4文件,这样导致观看回放的时候要频繁的切换视频,特别累。
如果能有办法将他一小时为单位合成就好了,了解了python可以轻松实现,所以尝试了一番,效果还不错,另外使用Python编程的感觉真的很爽。
1. 安装moviepy 1.1 通过命令行进入python安装目录 使用”Win+R”快捷键打开“运行”,输入“cmd”命令进入命令行终端,使用“CD”命令切换到python根目录
1 2 3 D:\> cd D:\Program Files\python\python37D:\Program Files\python\python37>cd Scripts
1.2 使用pip安装moviepy pip版本必须为20.0.2才可用,如果是早期版本安装过程中会报错,提示需要升级版本,按照提示升级之后重新安装即可。
期间还遇到imageio_ffmpeg>=0.2.0安装失败的情况,提示下载超时,判断是网速的问题,多试几次就可以了。
1 D:\Program Files\python\python37\Scripts>pip.exe install moviepy
提示pip版本太低,使用提示的命令进行升级。
升级pip版本之后重新安装moviepy,安装成功。
2. 编写python脚本 2.1 分析 源文件目录结构如下:
由上图可见,小米摄像头将视频以分钟为单位进行存储,最小的归档单位为小时,目录命名规则为:YYYYMMDDHH,年月日时同时体现在目录名中,视频文件前几位命名规则为:xxMxxS。
这样的命名规则便于排序,合成视频之前先进行升序排序操作,以保证合成之后的视频时间上保持连续。
思路很简单,将最后一级目录当做程序输入参数来使用,遍历最后一级目录,获得所有需要合并的视频文件名称,再和固定路径(/CamerasAndSurveillanc/xiaomi_camera_videos/6490**** /)拼接,就可以得到完整的源文件名称序列。在对该序列进行拼接操作,输出到指定位置即可。
可以输出到video station视频目录中,这样就可以方便的通过NAS视频客户端进行查看。
2.2.编写脚本 具体的代码实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 ''' 用途:米家小白摄像机云台版视频文件按照“每分钟一文件,按小时归档”的方式存储, 不便于回放查看,需要设法将每日或者数日的视频文件合并成一个文件进行存储。 视频文件存放于NAS上,无需下载到本地进行操作。 ''' from moviepy.editor import *import osdef get_name (path, source_path) : print("获取文件列表……" ) path_var = [] for i in source_path: path_var.append(path + i) name_list = [] for j in path_var: for k in os.listdir(j): name_list.append(j+'\\' +k) name_list.sort() for i in name_list: print(i) return (name_list) def combine (name_list, purpose_path, source_path) : print("合并文件……" ) source = [] for i in name_list: j = VideoFileClip(i) source.append(j) for i in source_path: purpose_path += i purpose_path += "(combine)" + ".mp4" purpose_flle = concatenate_videoclips(source) purpose_flle.to_videofile(purpose_path,fps=25 , remove_temp=True ) if __name__ == "__main__" : path = "G:\\video\\xiaomi\\6490********\\" source_path = [ "2020041617" , "2020041618" ] purpose_path = "G:\\video\\xiaomi\\" name_list = get_name(path, source_path) combine(name_list, purpose_path, source_path)
2.3 执行结果 使用VSCode编辑和运行脚本,在本地成功执行了测试,程序输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 获取文件列表…… G:\video\xiaomi\6490 ********\2020041617 \01 M47S_1587027707.mp4 G:\video\xiaomi\6490 ********\2020041617 \03 M19S_1587027799.mp4 G:\video\xiaomi\6490 ********\2020041617 \04 M53S_1587027893.mp4 G:\video\xiaomi\6490 ********\2020041617 \07 M17S_1587028037.mp4 G:\video\xiaomi\6490 ********\2020041617 \08 M20S_1587028100.mp4 G:\video\xiaomi\6490 ********\2020041617 \09 M44S_1587028184.mp4 G:\video\xiaomi\6490 ********\2020041617 \10 M41S_1587028241.mp4 G:\video\xiaomi\6490 ********\2020041617 \13 M32S_1587028412.mp4 G:\video\xiaomi\6490 ********\2020041617 \14 M53S_1587028493.mp4 G:\video\xiaomi\6490 ********\2020041617 \15 M54S_1587028554.mp4 G:\video\xiaomi\6490 ********\2020041617 \17 M31S_1587028651.mp4 G:\video\xiaomi\6490 ********\2020041617 \19 M33S_1587028773.mp4 G:\video\xiaomi\6490 ********\2020041617 \22 M11S_1587028931.mp4 G:\video\xiaomi\6490 ********\2020041617 \23 M10S_1587028990.mp4 G:\video\xiaomi\6490 ********\2020041617 \24 M09S_1587029049.mp4 G:\video\xiaomi\6490 ********\2020041617 \26 M29S_1587029189.mp4 G:\video\xiaomi\6490 ********\2020041617 \31 M08S_1587029468.mp4 G:\video\xiaomi\6490 ********\2020041617 \32 M36S_1587029556.mp4 G:\video\xiaomi\6490 ********\2020041617 \34 M38S_1587029678.mp4 G:\video\xiaomi\6490 ********\2020041617 \35 M44S_1587029744.mp4 G:\video\xiaomi\6490 ********\2020041617 \36 M44S_1587029804.mp4 G:\video\xiaomi\6490 ********\2020041617 \38 M13S_1587029893.mp4 G:\video\xiaomi\6490 ********\2020041617 \39 M11S_1587029951.mp4 G:\video\xiaomi\6490 ********\2020041617 \41 M09S_1587030069.mp4 G:\video\xiaomi\6490 ********\2020041617 \42 M08S_1587030128.mp4 G:\video\xiaomi\6490 ********\2020041617 \48 M13S_1587030493.mp4 G:\video\xiaomi\6490 ********\2020041617 \50 M02S_1587030602.mp4 G:\video\xiaomi\6490 ********\2020041617 \54 M40S_1587030880.mp4 G:\video\xiaomi\6490 ********\2020041617 \57 M34S_1587031054.mp4 G:\video\xiaomi\6490 ********\2020041617 \58 M34S_1587031114.mp4 G:\video\xiaomi\6490 ********\2020041617 \59 M49S_1587031189.mp4 G:\video\xiaomi\6490 ********\2020041618 \02 M17S_1587031337.mp4 G:\video\xiaomi\6490 ********\2020041618 \04 M00S_1587031440.mp4 G:\video\xiaomi\6490 ********\2020041618 \06 M12S_1587031572.mp4 G:\video\xiaomi\6490 ********\2020041618 \10 M18S_1587031818.mp4 G:\video\xiaomi\6490 ********\2020041618 \11 M32S_1587031892.mp4 G:\video\xiaomi\6490 ********\2020041618 \12 M50S_1587031970.mp4 G:\video\xiaomi\6490 ********\2020041618 \14 M19S_1587032059.mp4 G:\video\xiaomi\6490 ********\2020041618 \17 M19S_1587032239.mp4 G:\video\xiaomi\6490 ********\2020041618 \18 M25S_1587032305.mp4 G:\video\xiaomi\6490 ********\2020041618 \19 M34S_1587032374.mp4 G:\video\xiaomi\6490 ********\2020041618 \20 M33S_1587032433.mp4 G:\video\xiaomi\6490 ********\2020041618 \21 M43S_1587032503.mp4 G:\video\xiaomi\6490 ********\2020041618 \22 M42S_1587032562.mp4 G:\video\xiaomi\6490 ********\2020041618 \24 M28S_1587032668.mp4 G:\video\xiaomi\6490 ********\2020041618 \27 M23S_1587032843.mp4 G:\video\xiaomi\6490 ********\2020041618 \28 M51S_1587032931.mp4 G:\video\xiaomi\6490 ********\2020041618 \30 M37S_1587033037.mp4 G:\video\xiaomi\6490 ********\2020041618 \31 M34S_1587033094.mp4 G:\video\xiaomi\6490 ********\2020041618 \33 M17S_1587033197.mp4 G:\video\xiaomi\6490 ********\2020041618 \34 M21S_1587033261.mp4 G:\video\xiaomi\6490 ********\2020041618 \36 M28S_1587033388.mp4 G:\video\xiaomi\6490 ********\2020041618 \37 M26S_1587033446.mp4 G:\video\xiaomi\6490 ********\2020041618 \38 M40S_1587033520.mp4 G:\video\xiaomi\6490 ********\2020041618 \40 M52S_1587033652.mp4 G:\video\xiaomi\6490 ********\2020041618 \41 M50S_1587033710.mp4 G:\video\xiaomi\6490 ********\2020041618 \42 M50S_1587033770.mp4 G:\video\xiaomi\6490 ********\2020041618 \44 M47S_1587033887.mp4 G:\video\xiaomi\6490 ********\2020041618 \46 M01S_1587033961.mp4 G:\video\xiaomi\6490 ********\2020041618 \48 M41S_1587034121.mp4 G:\video\xiaomi\6490 ********\2020041618 \49 M38S_1587034178.mp4 G:\video\xiaomi\6490 ********\2020041618 \50 M47S_1587034247.mp4 G:\video\xiaomi\6490 ********\2020041618 \51 M47S_1587034307.mp4 G:\video\xiaomi\6490 ********\2020041618 \52 M47S_1587034367.mp4 G:\video\xiaomi\6490 ********\2020041618 \55 M29S_1587034529.mp4 G:\video\xiaomi\6490 ********\2020041618 \56 M54S_1587034614.mp4 G:\video\xiaomi\6490 ********\2020041618 \58 M25S_1587034705.mp4 合并文件…… Moviepy - Building video G:\video\xiaomi\20200416172020041618 (combine).mp4. MoviePy - Writing audio in 20200416172020041618 (combine)TEMP_MPY_wvf_snd.mp3 MoviePy - Done. Moviepy - Writing video G:\video\xiaomi\20200416172020041618 (combine).mp4 Moviepy - Done ! Moviepy - video ready G:\video\xiaomi\20200416172020041618 (combine).mp4 PS F:\portoflove\3 Frank\Code\VScode>
本文作者原创,转载请注明出处!