首頁 > 軟體

Python實現視訊自動打碼的範例程式碼

2022-04-08 13:00:43

序言

我們在觀看視訊的時候,有時候會出現一些奇怪的馬賽克,影響我們的觀影體驗,那麼這些馬賽克是如何精確的加上去的呢?

本次我們就來用Python實現對視訊自動打碼!

準備工作

環境咱們還是使用 Python3.8 和 pycharm2021 即可

實現原理

將視訊分為音訊和畫面;

畫面中出現人臉和目標比對,相應人臉進行打碼;

處理後的視訊新增聲音;

模組

手動安裝一下 cv2 模組 ,pip install opencv-python 安裝

素材工具

我們需要安裝一下 ffmpeg 音視訊轉碼工具

程式碼解析

匯入需要使用的模組

import cv2  
import face_recognition  # 臉部辨識庫  99.7%    cmake  dlib  face_recognition
import subprocess

將視訊轉為音訊

def video2mp3(file_name):
    """
    :param file_name: 視訊檔路徑
    :return:
    """
    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'):
    """
    :param input_video: 需打碼的視訊
    :param output_video: 打碼後的視訊
    :param mask_path: 打碼圖片
    :return:
    """
    # 讀取圖片
    mask = cv2.imread(mask_path)
    # 讀取視訊
    cap = cv2.VideoCapture(input_video)
    # 視訊  fps  width  height
    v_fps = cap.get(5)
    v_width = cap.get(3)
    v_height = cap.get(4)

    # 設定寫入視訊引數  格式MP4
    # 畫面大小
    size = (int(v_width), int(v_height))
    fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')

    # 輸出視訊
    out = cv2.VideoWriter(output_video, fourcc, v_fps, size)

    # 已知人臉
    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:
            # 檢測人臉
            # 人臉區域
            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]

                    # 對比人臉
                    results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
                    # [True]
                    # 貼圖
                    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

音訊新增到畫面

def video_add_mp3(file_name, mp3_file):
    """
    :param file_name: 視訊畫面檔案
    :param mp3_file:  視訊音訊檔
    :return:
    """
    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)

完整程式碼

import cv2 
import face_recognition  # 臉部辨識庫  99.7%    cmake  dlib  face_recognition
import subprocess

def 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'):

    # 讀取圖片
    mask = cv2.imread(mask_path)
    # 讀取視訊
    cap = cv2.VideoCapture(input_video)
    # 視訊  fps  width  height
    v_fps = cap.get(5)
    v_width = cap.get(3)
    v_height = cap.get(4)

    # 設定寫入視訊引數  格式MP4
    # 畫面大小
    size = (int(v_width), int(v_height))
    fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')

    # 輸出視訊
    out = cv2.VideoWriter(output_video, fourcc, v_fps, size)

    # 已知人臉
    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:
            # 檢測人臉
            # 人臉區域
            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]

                    # 對比人臉
                    results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
                    # [True]
                    # 貼圖
                    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


def 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')

兄弟們,快去試試吧!

到此這篇關於Python實現視訊自動打碼的範例程式碼的文章就介紹到這了,更多相關Python視訊打碼內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com