我需要将任意大小的视频文件分割成最大大小不超过 75 MB 的各个部分。我找到了这段代码,但它不起作用:
import cv
capture = cv.CaptureFromFile(filename)
while Condition1:
# Need a frame to get the output video dimensions
frame = cv.RetrieveFrame(capture) # Will return None if there are no frames
# New video file
video_out = cv.CreateVideoWriter(output_filenameX, CV_FOURCC('M','J','P','G'), capture.fps, frame.size(), 1)
# Write the frames
cv.WriteFrame(video_out, frame)
while Condition2:
frame = cv.RetrieveFrame(capture) # Will return None if there are no frames
cv.WriteFrame(video_out, frame)
这是我刚刚使用 MoviePy 创建的脚本。但不是除以字节大小,而是除以视频长度。因此,如果您将
divide_into_count
设置为 5 并且您有一个长度为 22 分钟的视频,那么您将获得长度为 5, 5, 5, 5, 2 分钟的视频。
from moviepy.editor import VideoFileClip
from time import sleep
full_video = "full.mp4"
current_duration = VideoFileClip(full_video).duration
divide_into_count = 5
single_duration = current_duration/divide_into_count
current_video = f"{current_duration}.mp4"
while current_duration > single_duration:
clip = VideoFileClip(full_video).subclip(current_duration-single_duration, current_duration)
current_duration -= single_duration
current_video = f"{current_duration}.mp4"
clip.to_videofile(current_video, codec="libx264", temp_audiofile='temp-audio.m4a', remove_temp=True, audio_codec='aac')
print("-----------------###-----------------")
Avne Desta 的解决方案(非常棒,对我很有帮助), 有一些细微的变化 以我的拙见,我认为这是更具可读性的代码(真的没有冒犯!),因为它不会反转,并且有一些小的命名内容。 它使用更方便的文件名(恕我直言),part_1、part_2 等。 它分为固定时间块。
所有积分均归原创者所有,而不是我!
full_video = r"20secs.mp4"
partDura = 8 # duration of a part in seconds
from moviepy.editor import VideoFileClip
from time import sleep
fullDura = VideoFileClip(full_video).duration
startPos = 0
i = 1
while True:
endPos = startPos + partDura
if endPos > fullDura:
endPos = fullDura
clip = VideoFileClip(full_video).subclip(startPos, endPos)
part_name = "part_"+str(i)+".mp4"
clip.to_videofile(part_name, codec="libx264", temp_audiofile='temp-audio.m4a', remove_temp=True, audio_codec='aac')
print("part ",i,"done")
i += 1
startPos = endPos # jump to next clip
if startPos >= fullDura:
break
您可以使用此工作示例来获取视频的两半,您 可以使用 'divide_into_count = 2' 相应地调整代码 对于多于 2 个视频部分的声明,请注意最后一个声明 该功能会删除原始输入视频文件
from moviepy.editor import VideoFileClip
import os
def split_videos(unsplit_videos_directory):
files = [file_name for file_name in os.listdir(unsplit_videos_directory) if file_name.endswith(".mp4")]
for file_name in files:
input_video_path = os.path.join(unsplit_videos_directory, file_name)
clip = VideoFileClip(input_video_path)
current_duration = clip.duration
divide_into_count = 2 # You can adjust this value as needed
single_duration = current_duration / divide_into_count
while current_duration > single_duration:
# Split the video into two equal halves
subclip = clip.subclip(current_duration - single_duration, current_duration)
# Save each subclip with the original filename and appropriate suffix
file_base_name, file_extension = os.path.splitext(file_name)
first_half_filename = os.path.join(unsplit_videos_directory, f"{file_base_name}_part2{file_extension}")
second_half_filename = os.path.join(unsplit_videos_directory, f"{file_base_name}_part1{file_extension}")
subclip.to_videofile(first_half_filename, codec="libx264", audio_codec="aac")
subclip.close() # Close the subclip before creating the second one
# Create the second subclip
subclip = clip.subclip(current_duration - 2 * single_duration, current_duration - single_duration)
subclip.to_videofile(second_half_filename, codec="libx264", audio_codec="aac")
# Close the second subclip
subclip.close()
# Update current_duration for the next iteration
current_duration -= single_duration
print(f"Video split and saved: {first_half_filename} and {second_half_filename}")
# Remove the original video
os.remove(input_video_path)
# Example usage
unsplit_videos_directory = "/path/to/unsplit_videos"
split_videos(unsplit_videos_directory)