Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How can Python synthesize multiple pictures into video and add background music?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how Python can synthesize multiple pictures into video and add background music". In daily operation, it is believed that many people have doubts about how Python can synthesize multiple pictures into video and add background music. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "how can Python synthesize multiple pictures into video and add background music"! Next, please follow the editor to study!

The idea to achieve: read the prepared pictures through opencv, and set the number of frames and other parameters into a silent video.

Finally, the background music is added to the video through moviepy editing.

Before you begin, you still need to explain the source of non-standard libraries, because the names of some libraries are not necessarily the same as the names of the modules that need to be imported.

Import os # python standard library, no installation, for system file operation related import cv2 # python non-standard library, pip install opencv-python multimedia processing from PIL import Image # python non-standard library, pip install pillow, image processing import moviepy.editor as mov # python non-standard library, pip install moviepy, multimedia editing

What module to use has been described, if not, then install it. The first step is to get the picture material ready to write how to convert the picture into video.

Def image_to_video (image_path Media_path):''picture composite video function: param image_path: picture path: param media_path: composite video save path: return:''# get all the picture names under the picture path image_names = os.listdir (image_path) # sort the extracted picture names image_names.sort (key=lambda n) : int (n [:-4]) # sets the write format fourcc = cv2.VideoWriter_fourcc ('M') 'frames per second, fps = 2 # due to the small number of pictures The number of frames set here is relatively low # read the first picture to get the size, because the image size that needs to be converted into video is the same image = Image.open (image_path + image_names [0]) # initialize the media write object media_writer = cv2.VideoWriter (media_path, fourcc, fps, image.size) # traverse the picture Add each picture to the video for image_name in image_names: im = cv2.imread (os.path.join (image_path, image_name)) media_writer.write (im) print (image_name, 'merge complete!') # release media writing object media_writer.release () print ('silent video writing complete!')

Add background music to the synthesized silent video, which can choose to extract audio from another video. Finally, the extracted audio file is added to the silent video.

Def set_music ():''synthetic video setting background music function: return:' 'print (' start adding background music!') # initialize the video file object clip = mov.VideoFileClip ('. / media.mp4') # extract a piece of background music from a video audio = mov.AudioFileClip ('. / source.mp4') .subclip (0 83) # write background music to .mp3 file audio.write_audiofile ('. / background.mp3') # add background music to the synthesized silent video clip = clip.set_audio (audio) # Save video clip.write_videofile ('. / media.mp4') print ('background music added complete!') # image_to_video ('. / images/','. / media.mp4') set_music ()

The above code block has been tested with the material, and can be directly used by copy without any error reports.

At this point, the study on "how to synthesize multiple pictures into video and add background music to Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report