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 to use Python to generate Nine Miyagi Video

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use Python to generate nine-grid video". In daily operation, I believe many people have doubts about how to use Python to generate nine-grid video. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about how to use Python to generate nine-grid video. Next, please follow the editor to study!

Prepare for

Before starting the actual combat, use pip to install 2 dependencies, which are:

1. Video processing depends on moviepy

2. Image processing depends on PIL.

# install two dependencies # Video processing pip3 install moviepy # Image processing depends on pip3 install Pillow

Let's have a real fight.

Before the actual combat, prepare an original video material.

Here are six steps to convert the original video into a nine-grid video.

1. Create a new processing folder

Create a new temporary folder and a video output folder

Def mkdir_folder (file_path): "" create a folder, create it if it does not exist; otherwise do not process: param file_path:: return: "" if os.path.exists (file_path): return os.mkdir (file_path) # New temporary folder and output folder mkdir_folder (self.path_temp) mkdir_folder (self.path_output)

2. Get the audio file and basic information of video.

First of all, according to the original video, use moviepy to build a VideoFileClip object to obtain the width, height, frame rate, duration and other information of the video.

Self.video_raw_clip = VideoFileClip (file_path) # wide and high self.video_width, selfself.video_height = self.video_raw_clip.w, self.video_raw_clip.h # frame rate selfself.fps = self.video_raw_clip.fps # Video duration selfself.during = self.video_raw_clip.duration

Next, the BGM audio object is extracted from the video and written to the file

Def get_audio_from_video (video_raw_clip, output_path): "" extract audio from video: param video_raw_clip: video Clip object: param output_path: output audio file full path: return: "" audio = video_raw_clip.audio audio.write_audiofile (output_path) return output_path

3. Deal with video frames

We use the iter_frames () method of the original video Clip object to get all the video frame pictures in a loop.

It should be pointed out that in order to ensure the convenience of subsequent video synthesis, the file names of video frames are ordered here.

I = 1 for frame in self.video_raw_clip.iter_frames (): image = Image.fromarray (frame) # temporary path (full path) for saving pictures in video frames frame_file_complete_path = self.path_temp + "d.jpg"% I + = 1

Each frame of the video is cropped into 9 pictures. We can explicitly specify the distance between the pictures, then calculate the width and height of the new canvas, and finally draw a picture with a white background.

# 1. Cut into 9 pictures Calculate the width and height of each picture item_width = int (self.video_width / 3) item_height = int (self.video_height / 3) # 2, new width and height item_width_new = self.video_width + self.item_space * 2 item_height_new = self.video_height + self.item_space * 2 # 3, rebuild a canvas background new_image = Image.new (image.mode, (item_width_new, item_height_new) Color='white')

Then, get the coordinate values of each area, add the interval offset for the 2nd and 3rd picture area horizontally and vertically, and paste it onto the newly created picture above.

# 4. Crop the picture and paste it into the new canvas # I: horizontal, j: vertical for i in range (0,3): for j in range (0,3): # crop region box = (j * item_width, I * item_height, (j + 1) * item_width, (I + 1) * item_height) # according to the region Crop picture crop_image = image.crop (box) # horizontal and vertical blocks 2 and 3, plus offset distance x = 0 if j = = 0 else (item_width + self.item_space) * j y = 0 if I = = 0 else (item_height + self.item_space) * I # 9 pictures, according to the coordinate values calculated above Paste into the background to new_image.paste (crop_image, (int (x), int (y)) # Save the picture to the local new_image.save (frame_file_complete_path)

4. A basket of pictures to re-synthesize the video

Convert the frame image generated in the previous step to video according to the frame rate of the original video.

It should be noted that in order to ensure that the generated video will not be confused, it is best to sort the frame pictures by name.

Def pics_to_video (pics_path, output_path, fps): "the picture is converted to video pics_to_video ('. /.. / gif_temp/','. /.. / video_temp/temp1.mp4', 20): param pics_path:: param output_path:: return:" image_paths = list (lambda x: pics_path + x Os.listdir (pics_path)) # Note: a sort must be done here Ensure that the order of all frames is the same image_paths = sort_strings_with_emb_numbers (image_paths) # filter out non-picture image_paths = list (lambda image_path: image_path.endswith ('.jpg'), image_paths) # Image clip class clip = ImageSequenceClip (image_paths, fpsfps=fps) clip.write_videofile (output_path)

5. Add BGM background music

Set the audio file of the original video to the video file generated in the previous step, and then write it to a new file

Def video_with_audio (path_video_raw, path_bgm_raw, output): "Video synthetic audio: return:" videoclip = VideoFileClip (path_video_raw) audioclip = AudioFileClip (path_bgm_raw) # set video and audio And write to the file to videoclip.set_audio (audioclip). Write_videofile (output, codec='libx264', audio_codec='aac', temp_audiofile='temp-audio.m4a') Remove_temp=True)

6. Delete temporary files

Delete the temporary files of the above video processing, including frame pictures and temporary video files, using shutil.

Def remove_folder (file_path): "" Delete folder: param file_path:: return: "shutil.rmtree (file_path) # Delete temporary file remove_folder (self.path_temp)

7. View the effect

At this point, the study on "how to use Python to generate Nine Palace grid video" 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