脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现视频自动打码的示例代码

Python实现视频自动打码的示例代码

2022-11-24 10:21嗨学编程 Python

我们在观看视频的时候,有时候会出现一些奇怪的马赛克,影响我们的观影体验,那么这些马赛克是如何精确的加上去的呢?本文就来为大家详细讲讲

序言

我们在观看视频的时候,有时候会出现一些奇怪的马赛克,影响我们的观影体验,那么这些马赛克是如何精确的加上去的呢?

Python实现视频自动打码的示例代码

本次我们就来用Python实现对视频自动打码!

准备工作

环境咱们还是使用 Python3.8 和 pycharm2021 即可

实现原理

将视频分为音频和画面;

画面中出现人脸和目标比对,相应人脸进行打码;

处理后的视频添加声音;

模块

手动安装一下 cv2 模块 ,pip install opencv-python 安装

素材工具

我们需要安装一下 ffmpeg 音视频转码工具

Python实现视频自动打码的示例代码

代码解析

导入需要使用的模块

?
1
2
3
import cv2 
import face_recognition  # 人脸识别库  99.7%    cmake  dlib  face_recognition
import subprocess

将视频转为音频

?
1
2
3
4
5
6
7
8
9
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)

打码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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

音频添加到画面

?
1
2
3
4
5
6
7
8
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)

完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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视频打码内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/fei347795790/article/details/124023651

延伸 · 阅读

精彩推荐
  • Pythonpython接口自动化使用requests库发送http请求

    python接口自动化使用requests库发送http请求

    这篇文章主要介绍了python接口自动化使用requests库发送http请求,HTTP协议 ,一个基于TCP/IP通信协议来传递数据,包括html文件、图像、结果等,即是一个客户...

    测试框架师凃九7632022-08-08
  • Python教你如何使用Python快速爬取需要的数据

    教你如何使用Python快速爬取需要的数据

    学点数据爬虫基础能让繁琐的数据CV工作(Ctrl+C,Ctrl+V)成为自动化就足够了.作为一名数据分析师而并非开发工程师,需要掌握的爬虫必备的知识内容,能获...

    Mr数据杨6172021-11-19
  • PythonPython文件操作和数据格式详解(简单简洁)

    Python文件操作和数据格式详解(简单简洁)

    文本处理是脚本语言的强项,下面这篇文章主要给大家介绍了关于Python文件操作和数据格式的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参...

    Philosophy73272022-11-07
  • Python基于Python制作AI聊天软件的示例代码

    基于Python制作AI聊天软件的示例代码

    这篇文章主要为大家详细介绍了如何利用Python语言制作一个简易的AI聊天软件,可以实现自动聊天,文中的示例代码讲解详细,需要的可以参考一下...

    晋升阁4122022-07-05
  • PythonDjango网络框架之HelloDjango项目创建教程

    Django网络框架之HelloDjango项目创建教程

    这篇文章主要介绍了Django网络框架之HelloDjango项目创建,结合实例形式详细分析了Django框架创建HelloDjango项目的具体步骤与详细实现技巧,需要的朋友可以参考...

    水木·圳烜7922021-07-02
  • Python利用Python+OpenCV三步去除水印

    利用Python+OpenCV三步去除水印

    去水印需要用到的库:cv2、numpy,cv2是基于OpenCV的图像处理库,可以对图像进行腐蚀,膨胀等操作.numpy这是一个强大的处理矩阵和维度运算的库,,需要的朋友可以...

    yunyun云芸4942021-11-16
  • Pythonpython3 requests 各种发送方式详解

    python3 requests 各种发送方式详解

    这篇文章主要介绍了python3 requests 各种发送方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...

    hgdzw4422021-10-22
  • PythonPython collections模块的使用技巧

    Python collections模块的使用技巧

    Python的最大优势之一是其广泛的模块和软件包。这将Python的功能扩展到许多受欢迎的领域,包括机器学习、数据科学和Web开发等, 其中最好的模块之一是P...

    大江狗9302021-10-24