Python 合并mp4

小米监控都是分段Mp4格式,无法统一播放,查找可以通过python进行合并,具体方法如下:

1、 安装moviepy

C:\Users\XXXXX>pip install moviepy
Collecting moviepy
Using cached moviepy-1.0.3.tar.gz (388 kB)
Collecting decorator<5.0,>=4.0.2
Using cached decorator-4.4.2-py2.py3-none-any.whl (9.2 kB)
Collecting tqdm<5.0,>=4.11.2
Using cached tqdm-4.59.0-py2.py3-none-any.whl (74 kB)
Collecting requests<3.0,>=2.8.1
Using cached requests-2.25.1-py2.py3-none-any.whl (61 kB)
Collecting proglog<=1.0.0
Using cached proglog-0.1.9.tar.gz (10 kB)
Requirement already satisfied: numpy>=1.17.3 in c:\python38\lib\site-packages (from moviepy) (1.18.1)
Collecting imageio<3.0,>=2.5
Using cached imageio-2.9.0-py3-none-any.whl (3.3 MB)
Collecting imageio_ffmpeg>=0.2.0
Downloading imageio_ffmpeg-0.4.3-py3-none-win_amd64.whl (22.6 MB)
|████████████████████████████████| 22.6 MB 819 kB/s
Collecting certifi>=2017.4.17
Downloading certifi-2020.12.5-py2.py3-none-any.whl (147 kB)
|████████████████████████████████| 147 kB 595 kB/s
Collecting urllib3<1.27,>=1.21.1
Downloading urllib3-1.26.3-py2.py3-none-any.whl (137 kB)
|████████████████████████████████| 137 kB 96 kB/s
Collecting chardet<5,>=3.0.2
Downloading chardet-4.0.0-py2.py3-none-any.whl (178 kB)
|████████████████████████████████| 178 kB 273 kB/s
Collecting idna<3,>=2.5
Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
|████████████████████████████████| 58 kB 381 kB/s
Collecting pillow
Downloading Pillow-8.1.2-cp38-cp38-win_amd64.whl (2.2 MB)
|████████████████████████████████| 2.2 MB 656 kB/s
Using legacy 'setup.py install' for moviepy, since package 'wheel' is not installed.
Using legacy 'setup.py install' for proglog, since package 'wheel' is not installed.
Installing collected packages: decorator, tqdm, certifi, urllib3, chardet, idna, requests, proglog, pillow, imageio, imageio-ffmpeg, moviepy
Running setup.py install for proglog ... done
Running setup.py install for moviepy ... done
Successfully installed certifi-2020.12.5 chardet-4.0.0 decorator-4.4.2 idna-2.10 imageio-2.9.0 imageio-ffmpeg-0.4.3 moviepy-1.0.3 pillow-8.1.2 proglog-0.1.9 requests-2.25.1 tqdm-4.59.0 urllib3-1.26.3

 

2、python 脚本

from moviepy.editor import *
import os


L = []
for root,dirs,files in os.walk("c:\\2"):
  x = files.sort()
  print(x)
  for file in files:
    if os.path.splitext(file)[1] == '.mp4':
      # print(file)
      filePath = os.path.join(root,file)
      print(filePath)
      video = VideoFileClip(filePath)
      L.append(video)
#
final_clip = concatenate_videoclips(L)
final_clip.to_videofile("./target.mp4", fps=24, remove_temp=False)

 

3、运行结果

c:\2\56M45S_1596009405.mp4
c:\2\57M45S_1596009465.mp4
c:\2\58M46S_1596009526.mp4
c:\2\59M45S_1596009585.mp4
Moviepy - Building video ./target.mp4.
MoviePy - Writing audio in targetTEMP_MPY_wvf_snd.mp3
MoviePy - Done.
Moviepy - Writing video ./target.mp4

 

    

posted @ 2021-03-15 15:17  小木目心  阅读(771)  评论(0编辑  收藏  举报