随记-探究视频文件的 MD5

背景

上传视频被判定重复
思考:什么原因导致判定重复呢?是不是文件的MD5?

方案一

往视频中加入一些二进制数据,但不影响视频的效果,从而改变视频的MD5值
结果:尝试了几次,都失败了。如果有对视频编码比较熟悉的大佬,可以提供相关帮助将不胜感激。

方案二

调整:增加水印,从而改变MD5值

MD5


import hashlib

KB = 1024
MB = KB * 1024
GB = MB * 1024

def md5(file_path):
    with open(file_path, 'rb') as f:
        md5_hash = hashlib.md5()
        while True:
            block = f.read(GB)
            if not block:
                break
            md5_hash.update(block)
        return md5_hash.hexdigest()

增加水印12


import cv2
def video_rewriter(source_path, target_path, logo_path, logo_size=None):
    """
        
    """
    source_video = cv2.VideoCapture(source_path)
    fps = source_video.get(cv2.CAP_PROP_FPS)   # 帧率
    frame_count = source_video.get(cv2.CAP_PROP_FRAME_COUNT) # 帧数
    duration = frame_count / fps  # 计算视频的时长(秒)
    size = (int(source_video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(source_video.get(cv2.CAP_PROP_FRAME_HEIGHT)))  # 分辨率

    print(f"视频时长为: {int(duration)}秒")
    print(f"视频帧率为: {int(fps)}FPS")
    print(f"视频总帧数: {int(frame_count)}")
    print(f"视频分辨率: {size}" )

    target_video = cv2.VideoWriter(target_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, size)  
    
    image2 = cv2.imread(logo_path)
    if logo_size: 
        # 调整水印大小(100,100)
        image2 = cv2.resize(image2, logo_size)
    
    # 获取logo的行列、通道数
    rows, cols, channels = image2.shape

    count_index = 0
    while(source_video.isOpened()):
        success, frame = source_video.read()
        if success==True:

            count_index +=1
            if count_index % 100 ==0:
                print(f"当前帧数:{count_index}, 总帧数: {int(frame_count)}")
 
            # 从image1中切割出一个与image2大小相同的区域,作为ROI(Region of Interest)
            roi = frame[0:rows, 0:cols]

            # 将image2转化为灰度图像,以便于进行阈值化处理
            image2gray = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)

            # ret阈值被忽略,mask二值化图像。大于254的值赋值为255,小于254的值赋值为0
            ret, mask = cv2.threshold(image2gray, 254, 255, cv2.THRESH_BINARY)

            # 按位取反操作,0变255,255变0
            mask_reverse = cv2.bitwise_not(mask)

            # 按位与操作,保留掩膜之后的背景
            image1_background = cv2.bitwise_and(roi, roi, mask=mask)

            # 按位与操作,保留掩膜之后的logo
            image2_logo = cv2.bitwise_and(image2, image2, mask=mask_reverse)

            # 将背景和logo叠加
            result = cv2.add(image1_background, image2_logo)

            frame[0:rows, 0:cols] = result
            target_video.write(frame)

            # cv2.imshow('frame',frame)
            # if cv2.waitKey(1) & 0xFF == ord('q'):
            #     break
        else:
            break

    print(f"当前帧数:{count_index}, 总帧数: {int(frame_count)}")
    source_video.release()
    target_video.release()
    cv2.destroyAllWindows()

增加音频2

import moviepy.editor as mov

def set_music(audio_source_path, video_source_path, target_path):
    '''
    合成视频设置背景音乐函数
    :return:
    '''
    print('开始添加背景音乐!')
    # 初始化视频文件对象
    clip = mov.VideoFileClip(video_source_path)
    # 从某个视频中提取一段背景音乐
    audio = mov.AudioFileClip(audio_source_path) # .subclip(0, 83)
    # # 将背景音乐写入.mp3文件
    # audio.write_audiofile(f'{base_path}/background.mp3')
    # 向合成好的无声视频中添加背景音乐
    clip = clip.set_audio(audio)
    # 保存视频
    # clip.write_videofile(video_source_path) # 源文件
    clip.write_videofile(target_path)   # 写入新文件
    print('背景音乐添加完成!')

source_code


到此结  DragonFangQy 2023.12.23


  1. 【Python入门教程】基于OpenCV视频分解成图片+图片组合成视频(视频抽帧组帧) ↩︎

  2. 如何将多张图片合成mp4视频格式,并加入背景音乐… ↩︎ ↩︎

你可能感兴趣的:(python,opencv,MD5,视频编解码)