下载了一堆音频编辑软件,最后在合并的时候都提示要收费,所以只好写代码搞定了 首先用QQ影音确定需要裁减音乐的地方,比如我需要裁掉11:08到11:48,12:51到13:05,那么python编码如下:
代码语言:javascript复制from pydub import AudioSegment
song = AudioSegment.from_wav("our.wav")
first = song[:(11*60 6)*1000]
first.export("first.wav", format="wav")
second = song[(11*60 48)*1000:(12*60 51)*1000]
second.export("second.wav", format="wav")
third = song[(13*60 5)*1000:]
third.export("third.wav", format="wav")
ring_lists = AudioSegment.empty()
ring_lists = first
ring_lists = second
ring_lists = third
ring_lists.export("ring_lists.wav", format="wav")
前提是安装pip install pydub 我没有安装ffmpeg,因为去ffmpeg发现文件好大,下载太耗时 如果改mp3就是下面的方式:
代码语言:javascript复制from pydub import AudioSegment
song = AudioSegment.from_mp3("our.mp3")
first = song[:(8*60 0)*1000]
first.export("first.wav", format="mp3")
third = song[(8*60 16)*1000:]
third.export("third.wav", format="mp3")
ring_lists = AudioSegment.empty()
ring_lists = first
ring_lists = third
ring_lists.export("ring.mp3", format="mp3")
记得在path中增加设置:C:ffmpegbin 参考文章 1、把多个音频文件合成一个文件,并且中间加10s的静音的间隔 2、pydub简单介绍
mp4文件
参考python 视频剪裁,剪切,合并
代码语言:javascript复制pip install moviepy
代码语言:javascript复制from moviepy.editor import *
clip1 = VideoFileClip("1.mp4").subclip(0,158) #读取视频1.mp4,并截取0-158秒的内容
clip2 = VideoFileClip("1.mp4").subclip(188.209) # #读取视频1.mp4,并截取188-209秒的内容
final_clip = concatenate_videoclips([clip1,clip2]) #视频合并
final_clip.write_videofile("2.mp4")#视频写入2.mp4