In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use Python and EV to clip short videos". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First, convert the picture into video
You can convert pictures into videos through python's opencv library. It is important to set the frame rate to 30 to facilitate subsequent stitching with other videos, because different frame rates can lead to stitching errors.
Import cv2,os import matplotlib.pyplot as plt from PIL import Image canshu=0.5 # set the video resolution parameter img = Image.open ('China Bill Development report .jpg') img.save ('ddd.jpg') img = cv2.imread (' ddd.jpg') # set the size of an image as the specified size imgimgInfo = img.shape size = (int (imgInfo [1] * canshu), int (imgInfo [0] * canshu)) fourcc = cv2.VideoWriter_fourcc ('M') The parameters are: file path, videoWrite, file path, file path, file path. Specify encoder; frame rate; picture size Whether it is color or black file_list = os.listdir ('. / 1 racer file_list') # get the list in the folder for i in range (0meme Len (file_list)): fileName ='. / 1 hash picture fileList [I] img = Image.open (fileName) # resize the picture according to the sample picture (x00) Y00) = img.size XPS = x00*canshu YBOS = int (Y00 * XCOS / X00) imgimg = img.resize (size,Image.ANTIALIAS) img.save ('ddd.jpg') img = cv2.imread (' ddd.jpg') # read the image # because the time for a single picture is too short Each picture inserts 20 frames of for j in range (0Magne20): videoWrite.write (img) videoWrite.release ()
But if you want to do some simple animation of the picture, such as an animation where the picture is gradually shrinking, use EV clips to resize the picture, add a white background, and then export the video.
Short video editing with Python and EV tutorial short video editing with Python and EV
Note that you need to set the frame rate when exporting. If the time is too long, you can set the frame rate a little lower, and then use the python program to increase the playback speed, and the frame rate will be increased accordingly.
Import cv2 vdop = "4.mp4" # input video path cap = cv2.VideoCapture (vdop) fps = cap.get (cv2.CAP_PROP_FPS) # get the frame rate of the input video size = (int (cap.get (cv2.CAP_PROP_FRAME_WIDTH)), int (cap.get (cv2.CAP_PROP_FRAME_HEIGHT) # get the size of the input video fourcc = cv2.VideoWriter_fourcc '0') # These are the FOURCCs to compressed formats out_path = "8.mp4" # output 2x video path in avi format output_viedo = cv2.VideoWriter () fps = (29.5max 26.8) * fps # 2x processing # isColor: if the bit value is Ture The decoder will decode the color frame, otherwise it will use grayscale for color architecture output_viedo.open (out_path, fourcc, fps, size, isColor=True) rval = True while rval: rval, img = cap.read () # read the original video output_viedo.write (img) # write video frame output_viedo.release () cap.release () frame by frame
Video processed with opencv loses audio. We can also use ffmpeg to speed, can retain the sound, especially suitable for fine-tuning. To use FFmpeg, after installation, set the environment variable, and you can call FFmpeg in python.
Import os os.system ('ffmpeg-I 4.mp4-vf "setpts=2*PTS" 8.mp4') # Image twice as slow
Second, add video to audio as background music
Import subprocess,os mp4_f ='. / 2.avi' mp3_f ='. / background sound .mp3 'n_mp4_n =' new' + mp4_f.split ('/') [- 1] n_mp4_f = mp4_f.replace (mp4_f.split ('/') [- 1] N_mp4_n) com = print:\\ ffmpeg\\ bin\\ ffmpeg.exe-I "{mp3_f}"-I "{mp4_f}"'\ f'- acodec copy-vcodec copy "{n_mp4_f}" 'print (com) os.system (com)
3. Merge videos
1. Cut the video
Import os import subprocess,cv2 com = f'ffmpeg-I 5.mp4-c copy-t 00lv 00copy 20.0 output.mp4' # 20 seconds before intercept-ss 00Rule 00Rd 00.0 start position os.system (com)
two。 To merge different videos, first make sure that the frame rate is the same as the canvas size, so as to ensure that the merge does not go wrong. The following code adjusts the canvas. The video pictures with different shapes should be adjusted into a frame according to the proportion of the original video.
Import subprocess Os import cv2 # get canvas size of sample video_path = "2\ 1.mp4" cap = cv2.VideoCapture (video_path) frame_width = int (cap.get (cv2.CAP_PROP_FRAME_WIDTH)) # get video height frame_height = int (cap.get (cv2.CAP_PROP_FRAME_HEIGHT)) # get video width fps = cap.get (cv2.CAP_PROP_FPS) # video average frame Rate h0=frame_height w0=frame_width file_list = os.listdir ('3\\) # get the list of all files in the folder for i in file_list: # get the current video video_path ='3\\'+ I cap = cv2.VideoCapture (video_path) frame_width = int (cap.get (cv2.CAP_PROP_FRAME_WIDTH)) # get the video height, Width frame_height = int (cap.get (cv2.CAP_PROP_FRAME_HEIGHT)) h=frame_height w=frame_width if hlot > h0/w0: strn=f'ffmpeg-I% s-vf "scale= (% s*%s/%s):% s Pad=%s:%s: (% s-(% s*%s/%s)) / 2:0:black "% s vf% (video_path,w,h0,h,h0, w0recitation h0w0recitation w0recoveryI) else: strn=f'ffmpeg-I% s-vf" scale=%s:%s*%s/%s Pad=%s:%s:0: (% s-(% s*%s/%s)) / 2): black "% sails% (video_path,w0,w0,h,w, w0recitation h0, h0pime h0w0rect I) # zoomed in this picture = width: height Position = Total width: total height: position placed horizontally: position placed vertically os.system (strn)
3. Merge videos
Convert the video to ts format first, and then merge it, which has a higher success rate.
Import os lista='kaishiwizhi' # get these MP4 files first Convert to ts format for each in os.listdir (): if each [- 3:] in ['mp4','avi']: os.system (' ffmpeg.exe-I% s-c copy-vbsf h364_mp4toannexb% s)) listalista=lista + ('|% s.ts% (each [:-4])) listalista=lista.replace ('kaishiwizhi |',') import subprocess,cv2 # subprocess.call (cmd) Shell=True) com = f'ffmpeg-I "concat:%s"-c copy 333333.avi'%lista # com = f'mencoder-forceidx-of lavf-oac copy-ovc copy-o output.avinew 2.avi new 2.avi' print (com) os.system (com) "how to use Python and EV to clip short videos" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.