import ffmpy
def audio_duration(length):
hours = length // 3600 # calculate in hours
length %= 3600
mins = length // 60 # calculate in minutes
length %= 60
seconds = length # calculate in seconds
return int(hours), int(mins), int(seconds)
def int_to_str(x):
if x <10:
return '0'+str(x)
else:
return str(x)
from pydub import AudioSegment as pd
path = 'D:/'
filename = 'xx'+'.mp4'
song = pd.from_file(path+filename)
hours, mins, seconds = audio_duration(len(song)/1000)
for i in range(hours+1):
if i < hours:
s=int_to_str(i)+':00'+':00'
t=int_to_str(i+1)+':00'+':00'
elif i == hours:
s=int_to_str(i)+':00'+':00'
t=int_to_str(i)+':'+int_to_str(mins)+':'+int_to_str(seconds)
ff = ffmpy.FFmpeg(
inputs={path+filename: None},
outputs={path+filename+str(i)+'.mp4': [
'-ss', s,
'-t', t,
'-vcodec', 'copy',
'-acodec', 'copy'
]}
)
ff.run()
python-ffmpeg 切割大视频、音频 (时间切片)demo
最新推荐文章于 2024-03-17 17:18:29 发布