You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -182,6 +182,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
182
182
-[How to Reverse Videos in Python](https://www.thepythoncode.com/article/reverse-video-in-python). ([code](python-for-multimedia/reverse-video))
183
183
-[How to Extract Video Metadata in Python](https://www.thepythoncode.com/article/extract-media-metadata-in-python). ([code](python-for-multimedia/extract-video-metadata))
184
184
-[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))
186
186
187
187
For any feedback, please consider pulling requests.
# 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
+
ifnotend:
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
0 commit comments