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 realize automatic coding of Video by Python

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I would like to share with you the relevant knowledge about how to achieve automatic video coding in Python. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's learn about it.

Preparatory work

We can still use Python3.8 and pycharm2021 in the environment.

Realization principle

Divide video into audio and picture

The face is compared with the target in the screen, and the corresponding face is coded.

Add sound to the processed video

Module

Manually install the cv2 module, pip install opencv-python installation

Material tool

We need to install the ffmpeg audio and video transcoding tool

Code parsing

Import modules that need to be used

Import cv2 import face_recognition # face recognition Library 99.7% cmake dlib face_recognitionimport subprocess

Convert video to audio

Def video2mp3 (file_name): "": param file_name: video file path: return: "outfile_name = file_name.split ('.') [0] + '.mp3' cmd = 'ffmpeg-I' + file_name +'- f mp3' + outfile_name print (cmd) subprocess.call (cmd, shell=False)

Code

Def mask_video (input_video, output_video Mask_path='mask.jpg'): ": param input_video: video to be typed: param output_video: video after coding: param mask_path: coded picture: return:" # read picture mask = cv2.imread (mask_path) # read video cap = cv2.VideoCapture (input_video) # video fps width height v _ fps = cap.get (5) v_width = cap.get (3) v_height = cap.get (4) # set write video parameter format MP4 # picture size size = (int (v_width)) Int (v_height)) fourcc = cv2.VideoWriter_fourcc ('masked,' paired, '4percent,' v') # output video out = cv2.VideoWriter (output_video, fourcc, v_fps) Size) # known face known_image = face_recognition.load_image_file ('tmr.jpg') biden_encoding = face_recognition.face_encodings (known_image) [0] cap = cv2.VideoCapture (input_video) while (cap.isOpened ()): ret Frame = cap.read () if ret: # detect face # face region face_locations = face_recognition.face_locations (frame) for (top_right_y, top_right_x, left_bottom_y, left_bottom_x) in face_locations: print ((top_right_y, top_right_x, left_bottom_y) Left_bottom_x)) unknown_image = frame [top _ right_y-50:left_bottom_y + 50 Left_bottom_x-50:top_right_x + 50] if face_recognition.face_encodings (unknown_image)! = []: unknown_encoding = face_recognition.face_encodings (unknown_image) [0] # compare results = face_recognition.compare_faces ([biden_encoding]) Unknown_encoding) # [True] # Map if results = = [True]: mask = cv2.resize (mask, (top_right_x-left_bottom_x, left_bottom_y-top_right_y)) frame [top _ right_y:left_bottom_y Left_bottom_x:top_right_x] = mask out.write (frame) else: break

Add audio to the screen

Def video_add_mp3 (file_name, mp3_file): "": param file_name: video picture file: param mp3_file: video and audio file: "outfile_name = file_name.split ('.') [0] +'- f.mp4' subprocess.call ('ffmpeg-I' + file_name +'- I'+ mp3_file +'- strict-2-f mp4' + outfile_name Shell=False) complete code import cv2 import face_recognition # face recognition library 99.7% cmake dlib face_recognitionimport subprocessdef video2mp3 (file_name): outfile_name = file_name.split ('.') [0] + '.mp3' cmd = 'ffmpeg-I' + file_name +'- f mp3' + outfile_name print (cmd) subprocess.call (cmd, shell=False) def mask_video (input_video, output_video) Mask_path='mask.jpg'): # read picture mask = cv2.imread (mask_path) # read video cap = cv2.VideoCapture (input_video) # Video fps width height v_fps = cap.get (5) v_width = cap.get (3) v_height = cap.get (4) # set write video parameter format MP4 # picture size size = (int (v_width)) Int (v_height)) fourcc = cv2.VideoWriter_fourcc ('masked,' paired, '4percent,' v') # output video out = cv2.VideoWriter (output_video, fourcc, v_fps) Size) # known face known_image = face_recognition.load_image_file ('tmr.jpg') biden_encoding = face_recognition.face_encodings (known_image) [0] cap = cv2.VideoCapture (input_video) while (cap.isOpened ()): ret Frame = cap.read () if ret: # detect face # face region face_locations = face_recognition.face_locations (frame) for (top_right_y, top_right_x, left_bottom_y, left_bottom_x) in face_locations: print ((top_right_y, top_right_x, left_bottom_y) Left_bottom_x)) unknown_image = frame [top _ right_y-50:left_bottom_y + 50 Left_bottom_x-50:top_right_x + 50] if face_recognition.face_encodings (unknown_image)! = []: unknown_encoding = face_recognition.face_encodings (unknown_image) [0] # compare results = face_recognition.compare_faces ([biden_encoding]) Unknown_encoding) # [True] # Map if results = = [True]: mask = cv2.resize (mask, (top_right_x-left_bottom_x, left_bottom_y-top_right_y)) frame [top _ right_y:left_bottom_y Left_bottom_x:top_right_x] = mask out.write (frame) else: breakdef video_add_mp3 (file_name, mp3_file): outfile_name = file_name.split ('.') [0] +'- f.mp4' subprocess.call ('ffmpeg-I' + file_name +'- I'+ mp3_file +'- strict-2-f mp4' + outfile_name Shell=False) if _ _ name__ = ='_ main__': # 1. Video2mp3 ('cut.mp4') # 2. Mask_video (input_video='cut.mp4',output_video='output.mp4') # 3. Video_add_mp3 (file_name='output.mp4',mp3_file='cut.mp3') is all the content of the article "how to realize automatic video coding in Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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