Fundamentals 8 min read

Python Video Editing Made Easy: Extract Audio & Create Fun Clips with MoviePy

This guide walks you through installing MoviePy, extracting audio from MP4 files, trimming video segments, and stitching them together—including advanced effects like mirroring—to automate video editing tasks in Python, with complete code examples and explanations of container formats.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Video Editing Made Easy: Extract Audio & Create Fun Clips with MoviePy

Introduction

Half a month ago a colleague asked how to extract audio from a video and convert the speech to text in a Word document. The solution uses Baidu's speech‑recognition API, but first we need to separate video and audio tracks. This article introduces MoviePy, a Python library that simplifies video manipulation, including cutting, concatenating, and extracting audio.

Understanding Container Formats

Common video file extensions such as .mp4 , .avi , and .flv are container formats that bundle video frames, audio streams, subtitles, and metadata into a single file. Think of the container as a box that holds all these elements.

Environment Setup

Install the MoviePy library (which relies on ffmpeg ) with the following command:

<code>pip install moviepy</code>

During installation you may notice the ffmpeg dependency, a powerful tool for video encoding and decoding.

Practical Demonstration

We download two short video clips (a “wild wolf disco” clip and a dancing‑girl clip) and perform the following operations:

Trim a 5‑second segment from each clip.

Concatenate the trimmed clips.

Apply mirroring effects to create a “ghost‑clip” effect.

All code is executed in a Jupyter notebook.

Trim and export a clip

<code># Import required classes
from moviepy.editor import *
# Load video and cut 10‑15 seconds
clip = VideoFileClip("disco.mp4").subclip(10, 15)
# Save the trimmed segment
clip.write_videofile("disco_2.mp4")</code>

Concatenate two clips

<code>from moviepy.editor import VideoFileClip, concatenate_videoclips
clip1 = VideoFileClip("disco_2.mp4")
clip2 = VideoFileClip("disco_w_2.mp4")
finalclip = concatenate_videoclips([clip1, clip2])
finalclip.write_videofile("disco_final.mp4")</code>

Extract Audio from Video

Video files contain separate video and audio tracks. To extract the audio, simply access the audio attribute and write it to an audio file.

<code>from moviepy.editor import *
video = VideoFileClip('disco.mp4')   # Load video
audio = video.audio
audio.write_audiofile('disco.mp3')   # Save audio
</code>

Advanced “Ghost” Effect

By repeating a short segment, applying horizontal and vertical mirroring, and arranging the clips in a grid, we create a humorous “ghost” video.

<code>from moviepy.editor import *
clip_raw = VideoFileClip("disco_w.mp4")
clip_before = VideoFileClip("disco_w.mp4").subclip(0, 1)
clip_after = VideoFileClip("disco_w.mp4").subclip(4, 13)
clip1 = concatenate_videoclips([clip_before]*3 + [clip_after])
clip2 = clip1.fx(vfx.mirror_x)
clip3 = clip1.fx(vfx.mirror_y)
clip4 = clip3.fx(vfx.mirror_x)
videoclip = clips_array([[clip1, clip2], [clip3, clip4]])
audio = AudioFileClip("disco_w.mp4")
finalclip = videoclip.set_audio(audio)
finalclip.write_videofile("ghost-disco.mp4")
</code>

Conclusion

MoviePy provides a concise, programmatic way to perform common video editing tasks—cutting, concatenating, extracting audio, and applying effects—far faster than manual GUI tools. It is especially useful for batch processing large numbers of videos, such as automating content for short‑video platforms.

pythonVideo EditingAudio ExtractionCode TutorialMoviePy
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.