python剪切視頻與合并視頻的實(shí)現(xiàn)
windows10/python3.6環(huán)境需安裝imageio,ffmpeg(視頻中有教下載)文字中不體現(xiàn)太麻煩,看起來(lái)也不方便!
剪切代碼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) # 存儲(chǔ)文件選擇按鈕和選擇編輯框 self.target_btn = QPushButton('目標(biāo)路徑', 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) #截切開(kāi)始時(shí)間輸入框和提示 self.startLabel = QLabel(self) self.startLabel.move(30, 150) self.startLabel.resize(60,30) self.startLabel.setText("開(kāi)始秒") self.start_le = QLineEdit(self) self.start_le.move(120,150) self.start_le.resize(50,30) # 截切結(jié)束時(shí)間輸入框和提示 self.stopLabel = QLabel(self) self.stopLabel.move(230, 150) self.stopLabel.resize(60,30) self.stopLabel.setText("結(jié)束秒") self.stop_le = QLineEdit(self) self.stop_le.move(320,150) self.stop_le.resize(50,30) #保存按鈕,調(diào)取數(shù)據(jù)增加函數(shù)等 self.save_btn = QPushButton('開(kāi)始',self) self.save_btn.move(30, 210) self.save_btn.resize(140, 30) self.save_btn.clicked.connect(self.addNum) #執(zhí)行成功返回值顯示位置設(shè)置 self.result_le = QLabel(self) self.result_le.move(30, 270) self.result_le.resize(340, 30) #整體界面設(shè)置 self.setGeometry(400, 400, 400, 400) self.setWindowTitle('視頻剪切')#設(shè)置界面標(biāo)題名 self.show() # 打開(kāi)的視頻文件名稱 def select_source(self): target,fileType = QFileDialog.getOpenFileName(self, "選擇源文件", "C:/") self.source_le.setText(str(target)) #保存的視頻文件名稱,要寫(xiě)上后綴名 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()#獲取開(kāi)始剪切時(shí)間 stop_time = self.stop_le.text().strip()#獲取剪切的結(jié)束時(shí)間 video = VideoFileClip(source)#視頻文件加載 video = video.subclip(int(start_time), int(stop_time))#執(zhí)行剪切操作 video.to_videofile(target, fps=20, remove_temp=True)#輸出文件 self.result_le.setText("ok!")#輸出文件后界面返回OK self.result_le.setStyleSheet("color:red;font-size:40px")#設(shè)置OK顏色為紅色,大小為四十像素 self.result_le.setAlignment(Qt.AlignCenter)#OK在指定框內(nèi)居中 if __name__=="__main__": app = QApplication(sys.argv) ex = login() sys.exit(app.exec_())
合并視頻:python代碼
# 主要是需要moviepy這個(gè)庫(kù) from moviepy.editor import * import os # 定義一個(gè)數(shù)組 L = [] # 訪問(wèn) video 文件夾 (假設(shè)視頻都放在這里面) for root, dirs, files in os.walk("./video"): # 按文件名排序 files.sort() # 遍歷所有文件 for file in files: # 如果后綴名為 .mp4 if os.path.splitext(file)[1] == '.mp4': # 拼接成完整路徑 filePath = os.path.join(root, file) # 載入視頻 video = VideoFileClip(filePath) # 添加到數(shù)組 L.append(video) # 拼接視頻 final_clip = concatenate_videoclips(L) # 生成目標(biāo)視頻文件 final_clip.to_videofile("./target.mp4", fps=24, remove_temp=False)
在線觀看視頻更清楚學(xué)得更快更好!
到此這篇關(guān)于python剪切視頻與合并視頻的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python剪切合并視頻內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)html轉(zhuǎn)換為pdf報(bào)告(生成pdf報(bào)告)功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)html轉(zhuǎn)換為pdf報(bào)告功能,結(jié)合實(shí)例形式分析了Python使用pdfkit實(shí)現(xiàn)HTML轉(zhuǎn)換為PDF的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-05-05python基礎(chǔ)_文件操作實(shí)現(xiàn)全文或單行替換的方法
下面小編就為大家?guī)?lái)一篇python基礎(chǔ)_文件操作實(shí)現(xiàn)全文或單行替換的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09使用Python下載歌詞并嵌入歌曲文件中的實(shí)現(xiàn)代碼
這篇文章主要介紹了使用Python下載歌詞并嵌入歌曲文件中的實(shí)現(xiàn)代碼,需要借助eyed3模塊,需要的朋友可以參考下2015-11-11解決Tensorflow使用pip安裝后沒(méi)有model目錄的問(wèn)題
今天小編就為大家分享一篇解決Tensorflow使用pip安裝后沒(méi)有model目錄的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06python+appium自動(dòng)化測(cè)試之如何控制App的啟動(dòng)和退出
本文主要介紹了python+appium自動(dòng)化測(cè)試之如何控制App的啟動(dòng)和退出,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Python?GUI和游戲開(kāi)發(fā)從入門(mén)到實(shí)踐
GUI是圖形用戶界面的縮寫(xiě),圖形化的用戶界面對(duì)使用過(guò)計(jì)算機(jī)的人來(lái)說(shuō)應(yīng)該都不陌生,下面這篇文章主要給大家介紹了關(guān)于Python圖形用戶界面與游戲開(kāi)發(fā)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05