Skip to content

Commit 49b5302

Browse files
committed
add combining static image with audio to make a video tutorial
1 parent ad6d0ea commit 49b5302

File tree

7 files changed

+36
-0
lines changed

7 files changed

+36
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
9595
- [How to Change Text Color in Python](https://www.thepythoncode.com/article/change-text-color-in-python). ([code](general/printing-in-colors))
9696
- [How to Create a Watchdog in Python](https://www.thepythoncode.com/article/create-a-watchdog-in-python). ([code](general/directory-watcher))
9797
- [How to Extract Audio from Video in Python](https://www.thepythoncode.com/article/extract-audio-from-video-in-python). ([code](general/video-to-audio-converter))
98+
- [How to Combine a Static Image with Audio in Python](https://www.thepythoncode.com/article/add-static-image-to-audio-in-python). ([code](python-for-multimedia/add-photo-to-audio))
9899

99100

100101
- ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# [How to Combine a Static Image with Audio in Python](https://www.thepythoncode.com/article/add-static-image-to-audio-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from moviepy.editor import AudioFileClip, ImageClip
2+
3+
4+
def add_static_image_to_audio(image_path, audio_path, output_path):
5+
"""Create and save a video file to `output_path` after
6+
combining a static image that is located in `image_path`
7+
with an audio file in `audio_path`"""
8+
# create the audio clip object
9+
audio_clip = AudioFileClip(audio_path)
10+
# create the image clip object
11+
image_clip = ImageClip(image_path)
12+
# use set_audio method from image clip to combine the audio with the image
13+
video_clip = image_clip.set_audio(audio_clip)
14+
# specify the duration of the new clip to be the duration of the audio clip
15+
video_clip.duration = audio_clip.duration
16+
# set the FPS to 1
17+
video_clip.fps = 1
18+
# write the resuling video clip
19+
video_clip.write_videofile(output_path)
20+
21+
22+
23+
24+
if __name__ == "__main__":
25+
import argparse
26+
parser = argparse.ArgumentParser(description="Simple Python script to add a static image to an audio to make a video")
27+
parser.add_argument("image", help="The image path")
28+
parser.add_argument("audio", help="The audio path")
29+
parser.add_argument("output", help="The output video file path")
30+
args = parser.parse_args()
31+
add_static_image_to_audio(args.image, args.audio, args.output)
Loading
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
moviepy

0 commit comments

Comments
 (0)