欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用python進行視頻剪輯

 更新時間:2020年11月02日 16:03:03   作者:huapyuan  
這篇文章主要介紹了如何用python進行視頻剪輯,幫助大家更好的利用python處理視頻,感興趣的朋友可以了解下

一、目標

python,利用moviepy和pydub將一段視頻進行區(qū)間切割

二、源碼

import os
from moviepy.video.io.VideoFileClip import VideoFileClip
from pydub import AudioSegment


def clip_video(source_file, target_file, start_time, stop_time):
  """
  利用moviepy進行視頻剪切
  :param source_file: 原視頻的路徑,mp4格式
  :param target_file: 生成的目標視頻路徑,mp4格式
  :param start_time: 剪切的起始時間點(第start_time秒)
  :param stop_time: 剪切的結(jié)束時間點(第stop_time秒)
  :return:
  """
  validate_file(source_file)
  source_video = VideoFileClip(source_file)
  video = source_video.subclip(int(start_time), int(stop_time)) # 執(zhí)行剪切操作
  video.write_videofile(target_file) # 輸出文件


def clip_audio(source_file, target_file, start_time, stop_time):
  """
  利用pydub進行音頻剪切。pydub支持源文件為 mp4格式,因此這里的輸入可以與視頻剪切源文件一致
  :param source_file: 原視頻的路徑,mp4格式
  :param target_file: 生成的目標視頻路徑,mp4格式
  :param start_time: 剪切的起始時間點(第start_time秒)
  :param stop_time: 剪切的結(jié)束時間點(第stop_time秒)
  :return:
  """
  validate_file(source_file)
  audio = AudioSegment.from_file(source_file, "mp4")
  audio = audio[start_time * 1000: stop_time * 1000]
  audio_format = target_file[target_file.rindex(".") + 1:]
  audio.export(target_file, format=audio_format)


def combine_video_audio(video_file, audio_file, target_file, delete_tmp=False):
  """
  利用 ffmpeg將視頻和音頻進行合成
  :param video_file:
  :param audio_file:
  :param target_file:
  :param delete_tmp: 是否刪除剪切過程生成的原視頻/音頻文件
  :return:
  """
  validate_file(video_file)
  validate_file(audio_file)
  # 注:需要先指定音頻再指定視頻,否則可能出現(xiàn)無聲音的情況
  command = "ffmpeg -y -i {0} -i {1} -vcodec copy -acodec copy {2}".format(audio_file, video_file, target_file)
  os.system(command)
  if delete_tmp:
    os.remove(video_file)
    os.remove(audio_file)


def clip_handle(source_file, target_file, start_time, stop_time, tmp_path=None, delete_tmp=False):
  """
  將一個視頻文件按指定時間區(qū)間進行剪切
  :param source_file: 原視頻文件
  :param target_file: 目標視頻文件
  :param start_time: 剪切的起始時間點(第start_time秒)
  :param stop_time: 剪切的結(jié)束時間點(第stop_time秒)
  :param tmp_path: 剪切過程的文件存放位置
  :param delete_tmp: 是否刪除剪切生成的文件
  :return:
  """
  # 設(shè)置臨時文件名
  if tmp_path is None or not os.path.exists(tmp_path):
    # 如果沒有指定臨時文件路徑,則默認與目標文件的位置相同
    tmp_path = target_file[: target_file.rindex("/") + 1]
  target_file_name = target_file[target_file.rindex("/") + 1: target_file.rindex(".")]
  tmp_video = tmp_path + "v_" + target_file_name + ".mp4"
  tmp_audio = tmp_path + "a_" + target_file_name + ".mp4"

  # 執(zhí)行文件剪切及合成
  clip_video(source_file, tmp_video, start_time, stop_time)
  clip_audio(source_file, tmp_audio, start_time, stop_time)
  combine_video_audio(tmp_video, tmp_audio, target_file, delete_tmp)


def validate_file(source_file):
  if not os.path.exists(source_file):
    raise FileNotFoundError("沒有找到該文件:" + source_file)


def test_example():
  """
  測試例子
  :return:
  """
  root_path = 'XXX/videos/'
  video_name = "test.mp4"
  source_file = root_path + video_name
  start_time = 5
  stop_time = 6

  # 設(shè)置目標文件名
  target_name = str(start_time) + "_" + str(stop_time)
  target_file = root_path + "c_" + target_name + ".mp4"
  # 處理主函數(shù)
  clip_handle(source_file, target_file, start_time, stop_time)


if __name__ == "__main__":
  test_example()

三、遇到的問題

1. moviepy切割后的視頻沒有聲音

解決方案:通過pydub切割后再合并

2. 直接利用ffmpeg切割后,視頻會出現(xiàn)黑屏、時間區(qū)間不準確、分辨率低

解決方案:用了各種命令也沒有成功,所以放棄。。。

3. 合并時,不支持mp3、 wav等格式

解決方案:統(tǒng)一保存為mp4

以上就是用python進行視頻剪輯的詳細內(nèi)容,更多關(guān)于python 視頻剪輯的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 常用python數(shù)據(jù)類型轉(zhuǎn)換函數(shù)總結(jié)

    常用python數(shù)據(jù)類型轉(zhuǎn)換函數(shù)總結(jié)

    這篇文章主要介紹了常用的python數(shù)據(jù)類型轉(zhuǎn)換函數(shù),并用實際例子說明了這些函數(shù)的用法,需要的朋友可以參考下
    2014-03-03
  • PyCharm中Python解釋器如何選擇詳析

    PyCharm中Python解釋器如何選擇詳析

    這篇文章主要給大家介紹了關(guān)于PyCharm中Python解釋器如何選擇的相關(guān)資料,文中詳細分析了四種常見的Python環(huán)境管理工具,分別是venv、conda、pipenv和poetry,需要的朋友可以參考下
    2024-11-11
  • Python采用Django制作簡易的知乎日報API

    Python采用Django制作簡易的知乎日報API

    這篇文章主要為大家詳細介紹了Python采用Django制作簡易的知乎日報API,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 使用Gitee自動化部署python腳本的詳細過程

    使用Gitee自動化部署python腳本的詳細過程

    小編最近在自學python,在學習過程中有好多意向不到的收獲,真的很開心,今天重點給大家分享使用Gitee自動化部署python腳本的詳細過程,包括安裝環(huán)境搭建及一些注意事項,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • Python中range函數(shù)的基本用法完全解讀

    Python中range函數(shù)的基本用法完全解讀

    range函數(shù)大多數(shù)時常出現(xiàn)在for循環(huán)中,在for循環(huán)中可做為索引使用,下面這篇文章主要給大家介紹了關(guān)于Python中range函數(shù)的基本用法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-01-01
  • Python中 map()函數(shù)的用法詳解

    Python中 map()函數(shù)的用法詳解

    map( )函數(shù)在算法題目里面經(jīng)常出現(xiàn),map( )會根據(jù)提供的函數(shù)對指定序列做映射,在寫返回值等需要轉(zhuǎn)換的時候比較常用。這篇文章主要介紹了Python中 map()的用法,需要的朋友可以參考下
    2018-07-07
  • 數(shù)據(jù)驅(qū)動測試DDT之Selenium讀取Excel文件

    數(shù)據(jù)驅(qū)動測試DDT之Selenium讀取Excel文件

    這篇文章主要為大家介紹了數(shù)據(jù)驅(qū)動測試DDT之Selenium讀取Excel文件,
    2021-11-11
  • Python OpenCV一個窗口中顯示多幅圖像

    Python OpenCV一個窗口中顯示多幅圖像

    大家好,本篇文章主要講的是Python OpenCV一個窗口中顯示多幅圖像,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • python beautiful soup庫入門安裝教程

    python beautiful soup庫入門安裝教程

    Beautiful Soup是python的一個庫,最主要的功能是從網(wǎng)頁抓取數(shù)據(jù)。今天通過本文給大家分享python beautiful soup庫入門教程,需要的朋友參考下吧
    2021-08-08
  • Python之sklearn數(shù)據(jù)預(yù)處理中fit(),transform()與fit_transform()的區(qū)別

    Python之sklearn數(shù)據(jù)預(yù)處理中fit(),transform()與fit_transform()的區(qū)別

    這篇文章主要介紹了Python之sklearn數(shù)據(jù)預(yù)處理中fit(),transform()與fit_transform()的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評論