Python中使用moviepy進行視頻分割


場景

moviepy官網:

https://pypi.org/project/moviepy/

是一個用於視頻編輯的Python庫:切割、連接、標題插入、視頻合成、非線性編輯,視頻處理和定制效果的創建。

Github:

https://github.com/Zulko/moviepy

實現

首先你需要安裝moviepy

打開cmd輸入:

pip install moviepy

 

 

其次需要安裝ffmpeg,因為網絡問題,這里使用豆瓣鏡像

pip install imageio-ffmpeg -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

 

 

如果之前那么沒有安裝過PyQt5,你還需要:

pip install PyQt5 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

 然后新建python文件,輸入代碼

 

import imageio
imageio.plugins.ffmpeg.download()
import win_unicode_console
win_unicode_console.enable()
import sys,os
from PyQt5.QtCore import *
from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,QLabel,
                              QApplication,QFileDialog)
from moviepy.video.io.VideoFileClip  import VideoFileClip


class login(QWidget):
    def __init__(self):
        super(login,self).__init__()
        self.initUI()

    def initUI(self):
        #源文件選擇按鈕和選擇編輯框
        self.source_btn = QPushButton('源文件', self)
        self.source_btn.move(30, 30)
        self.source_btn.resize(60,30)
        self.source_btn.clicked.connect(self.select_source)
        self.source_le = QLineEdit(self)
        self.source_le.move(120, 30)
        self.source_le.resize(250,30)

        # 存儲文件選擇按鈕和選擇編輯框
        self.target_btn = QPushButton('目標路徑', self)
        self.target_btn.move(30, 90)
        self.target_btn.resize(60, 30)
        self.target_btn.clicked.connect(self.select_target)
        self.target_le = QLineEdit(self)
        self.target_le.move(120, 90)
        self.target_le.resize(250, 30)

        #截切開始時間輸入框和提示
        self.startLabel = QLabel(self)
        self.startLabel.move(30, 150)
        self.startLabel.resize(60,30)
        self.startLabel.setText("開始秒")
        self.start_le = QLineEdit(self)
        self.start_le.move(120,150)
        self.start_le.resize(50,30)

        # 截切結束時間輸入框和提示
        self.stopLabel = QLabel(self)
        self.stopLabel.move(230, 150)
        self.stopLabel.resize(60,30)
        self.stopLabel.setText("結束秒")
        self.stop_le = QLineEdit(self)
        self.stop_le.move(320,150)
        self.stop_le.resize(50,30)

        #保存按鈕,調取數據增加函數等
        self.save_btn = QPushButton('開始',self)
        self.save_btn.move(30, 210)
        self.save_btn.resize(140, 30)
        self.save_btn.clicked.connect(self.addNum)

 

        #執行成功返回值顯示位置設置
        self.result_le = QLabel(self)
        self.result_le.move(30, 270)
        self.result_le.resize(340, 30)


        #整體界面設置
        self.setGeometry(400, 400, 400, 400)
        self.setWindowTitle('視頻剪切')#設置界面標題名
        self.show()

    # 打開的視頻文件名稱
    def select_source(self):
        target,fileType = QFileDialog.getOpenFileName(self, "選擇源文件", "C:/")
        self.source_le.setText(str(target))
    #保存的視頻文件名稱,要寫上后綴名
    def select_target(self):
        target,fileType = QFileDialog.getSaveFileName(self, "選擇保存路徑", "C:/")
        self.target_le.setText(str(target))


    def addNum(self):
        source = self.source_le.text().strip()#獲取需要剪切的文件
        target = self.target_le.text().strip()#獲取剪切后視頻保存的文件
        start_time = self.start_le.text().strip()#獲取開始剪切時間
        stop_time = self.stop_le.text().strip()#獲取剪切的結束時間
        video = VideoFileClip(source)#視頻文件加載
        video = video.subclip(int(start_time), int(stop_time))#執行剪切操作
        video.to_videofile(target, fps=20, remove_temp=True)#輸出文件
        self.result_le.setText("ok!")#輸出文件后界面返回OK
        self.result_le.setStyleSheet("color:red;font-size:40px")#設置OK顏色為紅色,大小為四十像素
        self.result_le.setAlignment(Qt.AlignCenter)#OK在指定框內居中

if __name__=="__main__":
    app = QApplication(sys.argv)
    ex = login()
    sys.exit(app.exec_())

 

 

效果


 

 

 

選擇要分割的視頻以及目標路徑(帶后綴),然后設置開始與結束時間,單位為秒。

 

 

點擊開始

 

 

完成之后,找到設置的目標路徑

可能出現的問題:

Python中使用pip安裝庫時提示:遠程主機強迫關閉了一個現有的連接:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100851841

imageio.ffmpeg.download() has been deprecated. Use 'pip install im ageio-ffmpeg' instead.':

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100853266

Imageio: 'ffmpeg-win32-v3.2.4.exe' was not found on your computer; downloading it now.:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100857437

ffmpeg-win32-v3.2.4exe.zip:

https://download.csdn.net/download/badao_liumang_qizhi/11747539

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM