自動コーディング-しかしPythonで

前面に書かれたナンセンス

私たちがビデオを見るとき、私たちの視聴体験に影響を与えるいくつかの奇妙なモザイクがあることがありますが、これらのモザイクはどのように正確に追加されますか?
ここに画像の説明を挿入

今回はPythonを使用してビデオを自動的にエンコードします!

準備オーケー

環境には引き続きPython3.8とpycharm2021を使用できます

実装の原則

  1. ビデオをオーディオと画像に分割します。
  2. 顔が画面に表示され、ターゲットが比較され、対応する顔がコード化されます。
  3. 処理されたビデオにサウンドを追加します。

モジュール

  • cv2モジュールを手動でインストールし、pipinstallopencv-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')

もう〜

おすすめ

転載: blog.csdn.net/m0_67575344/article/details/124048649