Skip to content

Commit 8674489

Browse files
committed
add add audio to video tutorial
1 parent 7e4a567 commit 8674489

File tree

6 files changed

+48
-1
lines changed

6 files changed

+48
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
182182
- [How to Reverse Videos in Python](https://www.thepythoncode.com/article/reverse-video-in-python). ([code](python-for-multimedia/reverse-video))
183183
- [How to Extract Video Metadata in Python](https://www.thepythoncode.com/article/extract-media-metadata-in-python). ([code](python-for-multimedia/extract-video-metadata))
184184
- [How to Record a Specific Window in Python](https://www.thepythoncode.com/article/record-a-specific-window-in-python). ([code](python-for-multimedia/record-specific-window))
185-
185+
- [How to Add Audio to Video in Python](https://www.thepythoncode.com/article/add-audio-to-video-in-python). ([code](python-for-multimedia/add-audio-to-video))
186186

187187
For any feedback, please consider pulling requests.
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# [How to Add Audio to Video in Python](https://www.thepythoncode.com/article/add-audio-to-video-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- `python add_audio_to_video_moviepy.py --help`
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeAudioClip
2+
import argparse
3+
4+
# make a command-line argument parser & add various parameters
5+
parser = argparse.ArgumentParser(description="Python script to add audio to video clip")
6+
parser.add_argument("-v", "--video-file", help="Target video file")
7+
parser.add_argument("-a", "--audio-file", help="Target audio file to embed with the video")
8+
parser.add_argument("-s", "--start", help="Start duration of the audio file, default is 0", default=0, type=int)
9+
parser.add_argument("-e", "--end", help="The end duration of the audio file, default is the length of the video file", type=int)
10+
parser.add_argument("-c", "--composite", help="Whether to add to the existing audio in the video", action="store_true", default=False)
11+
parser.add_argument("-f", "--volume-factor", type=float, default=1.0, help="The volume factor to multiply by the volume of the audio file, 1 means no change, below 1 will decrease volume, above will increase.")
12+
# parse the arguments
13+
args = parser.parse_args()
14+
video_file = args.video_file
15+
audio_file = args.audio_file
16+
start = args.start
17+
end = args.end
18+
composite = args.composite
19+
volume_factor = args.volume_factor
20+
# print the passed parameters, just for logging
21+
print(vars(args))
22+
# load the video
23+
video_clip = VideoFileClip(video_file)
24+
# load the audio
25+
audio_clip = AudioFileClip(audio_file)
26+
# use the volume factor to increase/decrease volume
27+
audio_clip = audio_clip.volumex(volume_factor)
28+
# if end is not set, use video clip's end
29+
if not end:
30+
end = video_clip.end
31+
# make sure audio clip is less than video clip in duration
32+
# setting the start & end of the audio clip to `start` and `end` paramters
33+
audio_clip = audio_clip.subclip(start, end)
34+
# composite with the existing audio in the video if composite parameter is set
35+
if composite:
36+
final_audio = CompositeAudioClip([video_clip.audio, audio_clip])
37+
else:
38+
final_audio = audio_clip
39+
# add the final audio to the video
40+
final_clip = video_clip.set_audio(final_audio)
41+
# save the final clip
42+
final_clip.write_videofile("final.mp4")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
moviepy
Binary file not shown.

0 commit comments

Comments
 (0)