The reason for building this tool was to solve a simple problem of cutting the video into a specified duration of time without me actually going through each cut manually. And this consumes a lot of time, if you are a daily content creator it really frustrates you when you want to cut video into exactly 15 to 30 seconds video for your WhatsApp story or social media stories. So this tool will help you cut the video effortlessly, without any manual work.
Of course, this is not an optimized version or I did not go through multiple iterations, it was my first try and so I believe the exists better approaches, but this is one of the first approaches that I considered while building this.
First, take the video file and provide the information where you want to perform cuts and other required information as shown below.
input_file = "videoplayback.mp4"
segments = [
{'start_time': 1, 'duration': 30, 'output_file': 'segment_1.mp4'},
{'start_time': 31, 'duration': 30, 'output_file': 'segment_2.mp4'},
{'start_time': 61, 'duration': 30, 'output_file': 'segment_3.mp4'},
]
I will be using opencv, one important thing about opencv is it only helped me cut the video into multiple copies but the audio fails to get attached along with the video.
To cut the audio, I have used ffmpeg to cut and attach the specific audio with the respective video.
Below is the implementation of the above logic
def cut_video(input_file, output_file, start_time_sec, duration_sec):
cap = cv2.VideoCapture(input_file)
if not cap.isOpened():
print(f"Error: Unable to open video file {input_file}")
return
fps = int(cap.get(cv2.CAP_PROP_FPS))
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
start_frame = int(start_time_sec * fps)
end_frame = start_frame + int(duration_sec * fps)
end_frame = min(end_frame, total_frames)
cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
temp_video_file = 'temp_video_cut.mp4'
out = cv2.VideoWriter(temp_video_file, fourcc, fps, (width, height))
current_frame = start_frame
while current_frame < end_frame:
ret, frame = cap.read()
if not ret:
print("Error: Unable to read frame")
break
out.write(frame)
current_frame += 1
cap.release()
out.release()
print(f"Video cut completed: {output_file}")
print("Now processing the audio")
temp_audio_file = 'temp_audio_cut.aac'
audio_extract_cmd = [
'ffmpeg', '-y',
'-i', input_file,
'-ss', str(start_time_sec),
'-t', str(duration_sec),
'-vn',
'-acodec', 'copy',
temp_audio_file
]
try:
subprocess.run(audio_extract_cmd, check=True)
print(f"Audio segment saved to: {temp_audio_file}")
except subprocess.CalledProcessError as e:
print(f"Error: failed to extract the audio file: {e}")
return
final_output_file = output_file
merge_cmd = [
'ffmpeg', '-y',
'-i', temp_video_file,
'-i', temp_audio_file,
'-c:v', 'copy',
'-c:a', 'aac',
'-strict', 'experimental',
final_output_file
]
try:
subprocess.run(merge_cmd, check=True)
print(f"Final video with audio saved to: {final_output_file}")
except subprocess.CalledProcessError as e:
print(f"Error: Failed to merge video and audio. {e}")
return
This code may look complex, but I think AI chatbots will definitely do great job in explaining well.
This is one of the interesting tools that I have built last week. Hope you got something out of it.