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

用python進(jìn)行視頻剪輯

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

一、目標(biāo)

python,利用moviepy和pydub將一段視頻進(jìn)行區(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進(jìn)行視頻剪切
  :param source_file: 原視頻的路徑,mp4格式
  :param target_file: 生成的目標(biāo)視頻路徑,mp4格式
  :param start_time: 剪切的起始時(shí)間點(diǎn)(第start_time秒)
  :param stop_time: 剪切的結(jié)束時(shí)間點(diǎn)(第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進(jìn)行音頻剪切。pydub支持源文件為 mp4格式,因此這里的輸入可以與視頻剪切源文件一致
  :param source_file: 原視頻的路徑,mp4格式
  :param target_file: 生成的目標(biāo)視頻路徑,mp4格式
  :param start_time: 剪切的起始時(shí)間點(diǎn)(第start_time秒)
  :param stop_time: 剪切的結(jié)束時(shí)間點(diǎn)(第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將視頻和音頻進(jìn)行合成
  :param video_file:
  :param audio_file:
  :param target_file:
  :param delete_tmp: 是否刪除剪切過(guò)程生成的原視頻/音頻文件
  :return:
  """
  validate_file(video_file)
  validate_file(audio_file)
  # 注:需要先指定音頻再指定視頻,否則可能出現(xiàn)無(wú)聲音的情況
  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):
  """
  將一個(gè)視頻文件按指定時(shí)間區(qū)間進(jìn)行剪切
  :param source_file: 原視頻文件
  :param target_file: 目標(biāo)視頻文件
  :param start_time: 剪切的起始時(shí)間點(diǎn)(第start_time秒)
  :param stop_time: 剪切的結(jié)束時(shí)間點(diǎn)(第stop_time秒)
  :param tmp_path: 剪切過(guò)程的文件存放位置
  :param delete_tmp: 是否刪除剪切生成的文件
  :return:
  """
  # 設(shè)置臨時(shí)文件名
  if tmp_path is None or not os.path.exists(tmp_path):
    # 如果沒(méi)有指定臨時(shí)文件路徑,則默認(rèn)與目標(biāo)文件的位置相同
    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("沒(méi)有找到該文件:" + source_file)


def test_example():
  """
  測(cè)試?yán)?
  :return:
  """
  root_path = 'XXX/videos/'
  video_name = "test.mp4"
  source_file = root_path + video_name
  start_time = 5
  stop_time = 6

  # 設(shè)置目標(biāo)文件名
  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()

三、遇到的問(wèn)題

1. moviepy切割后的視頻沒(méi)有聲音

解決方案:通過(guò)pydub切割后再合并

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

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

3. 合并時(shí),不支持mp3、 wav等格式

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

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

相關(guān)文章

  • 手把手帶你打造一個(gè)Pytest數(shù)據(jù)分離測(cè)試框架

    手把手帶你打造一個(gè)Pytest數(shù)據(jù)分離測(cè)試框架

    數(shù)據(jù)分離測(cè)試框架是一種測(cè)試框架設(shè)計(jì)模式,旨在將測(cè)試數(shù)據(jù)與測(cè)試邏輯分離,以提高測(cè)試用例的可維護(hù)性、可讀性和復(fù)用性,本文就來(lái)實(shí)現(xiàn)一下,感興趣的可以了解一下
    2024-03-03
  • 解決python3中解壓zip文件是文件名亂碼的問(wèn)題

    解決python3中解壓zip文件是文件名亂碼的問(wèn)題

    下面小編就為大家分享一篇解決python3中解壓zip文件是文件名亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • Python利用splinter實(shí)現(xiàn)瀏覽器自動(dòng)化操作方法

    Python利用splinter實(shí)現(xiàn)瀏覽器自動(dòng)化操作方法

    今天小編就為大家分享一篇Python利用splinter實(shí)現(xiàn)瀏覽器自動(dòng)化操作方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Python traceback模塊獲取異常信息的使用

    Python traceback模塊獲取異常信息的使用

    Python的traceback模塊提供了多種方法來(lái)獲取和展示異常的堆棧信息,本文主要介紹了Python traceback模塊獲取異常信息的使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • 用selenium解決滑塊驗(yàn)證碼的實(shí)現(xiàn)步驟

    用selenium解決滑塊驗(yàn)證碼的實(shí)現(xiàn)步驟

    驗(yàn)證碼作為一種自然人的機(jī)器人的判別工具,被廣泛的用于各種防止程序做自動(dòng)化的場(chǎng)景中,下面這篇文章主要給大家介紹了關(guān)于用selenium解決滑塊驗(yàn)證碼的實(shí)現(xiàn)步驟,需要的朋友可以參考下
    2023-02-02
  • Python中sort和sorted函數(shù)代碼解析

    Python中sort和sorted函數(shù)代碼解析

    這篇文章主要介紹了Python中sort和sorted函數(shù)代碼解析,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • Python?selenium?八種定位元素的方式

    Python?selenium?八種定位元素的方式

    這篇文章主要介紹了Python?selenium八種定位元素的方式,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • pytorch 查看cuda 版本方式

    pytorch 查看cuda 版本方式

    這篇文章主要介紹了pytorch 查看cuda 版本方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • Python 使用 consul 做服務(wù)發(fā)現(xiàn)示例詳解

    Python 使用 consul 做服務(wù)發(fā)現(xiàn)示例詳解

    這篇文章主要介紹了Python 使用 consul 做服務(wù)發(fā)現(xiàn)示例詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • 使用OpenCV實(shí)現(xiàn)道路車(chē)輛計(jì)數(shù)的使用方法

    使用OpenCV實(shí)現(xiàn)道路車(chē)輛計(jì)數(shù)的使用方法

    這篇文章主要介紹了使用OpenCV實(shí)現(xiàn)道路車(chē)輛計(jì)數(shù)的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評(píng)論