栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python使用moviepy剪切合并视频以及视频降噪

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python使用moviepy剪切合并视频以及视频降噪

先看文章python使用spleeter进行视频降噪,环境配置和所需文件完全一致。

1.mp4和2.mp4放到了百度网盘里

链接:https://pan.baidu.com/s/1jyKsR65QI7dpo9NIObGlhA?pwd=xyvk
提取码:xyvk

生成的:
smv1.mp4 12秒 截取1.mp4的第5-17秒
smv2.mp4 20秒 截取1.mp4的第5-17秒;截取1.mp4的0-10秒;合并两段视频 无背景声
smv3.mp4 20秒 截取1.mp4的第5-17秒;截取2.mp4的0-10秒;合并两段视频

from moviepy.editor import *
from moviepy.audio.AudioClip import AudioArrayClip
from spleeter.separator import Separator


def audioNoiseReduction(audio):
    """
    音频降噪(去除背景声保留人声)
    注:AudioClip包含AudioFileClip和AudioArrayClip两个子类
    :param audio: AudioClip或其子类对象
    :return: AudioClip或其子类对象
    """
    audiodata = audio.to_soundarray(fps=44100)
    prediction = Separator('spleeter:2stems').separate(audiodata)
    # 保存人声(其中fps 44100需和获取时一样,AudioFileClip('audio_example.wav')采用了缺省的默认fps 44100)
    vocals = AudioArrayClip(prediction['vocals'], 44100)
    return vocals


def videoNoiseReduction(video):
    """
    视频降噪(去除背景声保留人声)
    注:VideoClip包含VideoFileClip和DataVideoClip两个子类
    :param video: VideoClip或其子类对象
    :return: VideoClip或其子类对象
    """
    audio = audioNoiseReduction(video.audio)
    video_no = video.without_audio()
    video_red = video_no.set_audio(audio)
    audio.close()
    video_no.close()
    return video_red


def splitAndMergeVideo(pathList, savePath, timeList, noiseReduction=False):
    """
    截取&合并视频片段

    调用示例:
        splitAndMergeVideo('1.mp4', 'smv1.mp4', '00:05-00:17')
        splitAndMergeVideo(['1.mp4', '1.mp4'], 'smv2.mp4', ['00:05-00:17', '00:00-00:10'], noiseReduction=True)
        splitAndMergeVideo(['1.mp4', '2.mp4'], 'smv3.mp4', ['00:05-00:17', '00:00-00:10'])

    :param pathList: 视频路径列表(str/str list)
    :param savePath: 视频结果保存位置
    :param timeList: 欲截取的视频片段起止时间列表(str/str list),格式为'开始时间-截止时间',
        开始时间/截止时间需使用'分:秒'或者'时:分:秒'或者'秒’,时、分、秒均为2位整数,例如'12‘,'12:34','12:34:56'
    :param noiseReduction: 开启降噪(去除背景声保留人声)
    :return:  空
    """

    # 若为str,转为str list
    if isinstance(pathList, str):
        pathList = [pathList]
    if isinstance(timeList, str):
        timeList = [timeList]

    # 检查数据是否完整
    if len(pathList) != len(timeList):
        print('pathList 与 timeList 长度不匹配')

    # 解析并标准化起止时间格式,标准格式为'hh:mm:ss'
    startList = []
    endList = []
    for item in timeList:
        t = item.split('-')
        startList.append(t[0])
        endList.append(t[1])
    for index in range(len(startList)):
        if startList[index].count(':') == 0:
            startList[index] = '00:00:' + startList[index]
        elif startList[index].count(':') == 1:
            startList[index] = '00:' + startList[index]
    for index in range(len(endList)):
        if endList[index].count(':') == 0:
            endList[index] = '00:00:' + endList[index]
        elif endList[index].count(':') == 1:
            endList[index] = '00:' + endList[index]

    # 切割出每个视频的欲截取片段
    videoFiles = []
    for index in range(len(pathList)):
        videoFiles.append(VideoFileClip(pathList[index]).subclip(startList[index], endList[index]))

    # 合并片段,写入新文件,关闭每个片段的操作句柄
    finVideo = concatenate_videoclips(videoFiles)

    # 降噪
    if noiseReduction:
        finVideo2 = videoNoiseReduction(finVideo)
        finVideo2.write_videofile(savePath)
        finVideo2.close()
    else:
        finVideo.write_videofile(savePath)

    for item in videoFiles:
        item.close()
    finVideo.close()


if __name__ == '__main__':
    splitAndMergeVideo('1.mp4', 'smv1.mp4', '00:05-00:17')
    splitAndMergeVideo(['1.mp4', '1.mp4'], 'smv2.mp4', ['00:05-00:17', '00:00-00:10'], noiseReduction=True)
    splitAndMergeVideo(['1.mp4', '2.mp4'], 'smv3.mp4', ['00:05-00:17', '00:00-00:10'])

00:10'], noiseReduction=True)
    splitAndMergeVideo(['1.mp4', '2.mp4'], 'smv3.mp4', ['00:05-00:17', '00:00-00:10'])

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1022142.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号