Python slices video files into small segments of specified length
As shown in the code, create a folder in the same folder, divide the MP4 file into several small segments at 10-minute intervals, and adapt the video length.
from moviepy.editor import VideoFileClip
import pathlib
video_file = pathlib.Path(r'/Users/frank/Downloads/数据大爆炸无加密版本.mp4')
save_directory = video_file.parent / video_file.stem
save_directory.mkdir(parents=True, exist_ok=True)
duration_time = 30
full_video = video_file.as_posix()
current_duration = VideoFileClip(full_video).duration
divide_into_count = 25
single_duration = current_duration / divide_into_count
i = 0
current_video = (save_directory / (str(i) + '.mp4')).as_posix()
while current_duration > single_duration:
clip = VideoFileClip(full_video).subclip(current_duration - single_duration, current_duration)
current_duration -= single_duration
current_video = (save_directory / (str(i) + '.mp4')).as_posix()
clip.to_videofile(current_video, codec="libx264", temp_audiofile='temp-audio.m4a', remove_temp=True,
audio_codec='aac')
i += 1
print("-----------------###-----------------")
Leave a Reply
Want to join the discussion?Feel free to contribute!