视频隐写有问题,隐藏的消息总是丢失

发布于 2025-01-19 09:02:37 字数 3877 浏览 3 评论 0原文

朋友,我目前正在寻找一种视频隐写的方法。我成功地从视频文件中分割帧并将消息隐藏在其中。但是当我将这些帧组合成视频并尝试从隐藏视频中提取信息时,我总是失败。我猜这是视频压缩的问题。

这是我的代码。

from stegano import lsb
from os.path import isfile, join

import time  # install time ,opencv,numpy modules
import cv2
import numpy as np
import math
import os
import shutil
from moviepy.editor import *
from subprocess import call, STDOUT


def split_string(s_str, count=10):
    per_c = math.ceil(len(s_str)/count)
    c_cout = 0
    out_str = ''
    split_list = []
    for s in s_str:
        out_str += s
        c_cout += 1
        if c_cout == per_c:
            split_list.append(out_str)
            out_str = ''
            c_cout = 0
    if c_cout != 0:
        split_list.append(out_str)
    return split_list


def frame_extraction(video):
    if not os.path.exists("./tmp"):
        os.makedirs("tmp")    
    temp_folder = "./tmp"
    print("[INFO] tmp directory is created")

    vidcap = cv2.VideoCapture(video)
    count = 0

    while True:
        success, image = vidcap.read()
        if not success:
            break
        cv2.imwrite(os.path.join(temp_folder, "{:d}.png".format(count)), image)
        count += 1
        print("[INFO] frame {} is extracted".format(count))


def encode_string(input_string, root="./tmp/"):
    split_string_list = split_string(input_string)
    for i in range(0, len(split_string_list)):
        f_name = "{}{}.png".format(root, i)
        secret_enc = lsb.hide(f_name, split_string_list[i])
        secret_enc.save(f_name)
        print("[INFO] frame {} holds {}".format(f_name, lsb.reveal(f_name)))


def decode_string(video):
    frame_extraction(video)
    secret = []
    root = "./tmp/"
    for i in range(len(os.listdir(root))):
        f_name = "{}{}.png".format(root, i)
        print("[INFO] frame {} is decoding".format(f_name))
        secret_dec = lsb.reveal(f_name)
        if secret_dec == None:
            break
        secret.append(secret_dec)
    print("[INFO] secret is {}".format("".join(secret)))
    print(''.join([i for i in secret]))
    # clean_tmp()


def clean_tmp(path="./tmp"):
    if os.path.exists(path):
        shutil.rmtree(path)
        print("[INFO] tmp files are cleaned up")


def main():
    input_string = input("Enter the input string: ")
    f_name = input("enter the name of video: ")

    # 从源文件分离出帧
    frame_extraction(f_name)
    
    # 分离文件路径和扩展名
    file_path, file_extraction = os.path.splitext(f_name)
    
    # 创建输出音频文件
    audio_path = file_path + "_temp.mp3"
    video = VideoFileClip(f_name)
    video.audio.write_audiofile(audio_path)

    # 加密字符
    encode_string(input_string)

    # 从tmp文件夹的图片创建没有声音的视频
    fps=30
    img_root = r"./tmp/"
    # fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    video_file_path = file_path + "_temp.avi"
    # 获取tmp文件夹第一张视频的尺寸
    img = cv2.imread(img_root + "0.png")
    height, width, layers = img.shape
    size=(width,height)
    videoWriter = cv2.VideoWriter(video_file_path,fourcc=fourcc,fps=fps,frameSize=size)
    for i in range(len(os.listdir(img_root))):
        frame = cv2.imread(img_root+str(i)+'.png')
        videoWriter.write(frame)
    videoWriter.release()

    # 合并视频和音频     audio_path   video_file_path
    video = VideoFileClip(video_file_path)
    audio_clip = AudioFileClip(audio_path)
    video = video.set_audio(audio_clip)
    video.write_videofile(file_path + "_hide.avi")
    clean_tmp()


if __name__ == "__main__":
    while True:
        print("1.Hide a message in video 2.Reveal the secret from video")
        print("any other value to exit")
        choice = input()
        if choice == '1':
            main()
        elif choice == '2':
            decode_string(input("enter the name of video with extension: "))
        else:
            break

我尝试过mp4、avi、wov格式。但它们都不起作用。

如果您有任何想法或建议给我,我将非常感激

friend, i am currently looking for a way to video stegano. I successfully in splitting frames from video file and hide messages inside them. But when i combine these frames into video and trying to extract info from the hiden video, i always failed. I guess here is problem with video compression.

Here is my code.

from stegano import lsb
from os.path import isfile, join

import time  # install time ,opencv,numpy modules
import cv2
import numpy as np
import math
import os
import shutil
from moviepy.editor import *
from subprocess import call, STDOUT


def split_string(s_str, count=10):
    per_c = math.ceil(len(s_str)/count)
    c_cout = 0
    out_str = ''
    split_list = []
    for s in s_str:
        out_str += s
        c_cout += 1
        if c_cout == per_c:
            split_list.append(out_str)
            out_str = ''
            c_cout = 0
    if c_cout != 0:
        split_list.append(out_str)
    return split_list


def frame_extraction(video):
    if not os.path.exists("./tmp"):
        os.makedirs("tmp")    
    temp_folder = "./tmp"
    print("[INFO] tmp directory is created")

    vidcap = cv2.VideoCapture(video)
    count = 0

    while True:
        success, image = vidcap.read()
        if not success:
            break
        cv2.imwrite(os.path.join(temp_folder, "{:d}.png".format(count)), image)
        count += 1
        print("[INFO] frame {} is extracted".format(count))


def encode_string(input_string, root="./tmp/"):
    split_string_list = split_string(input_string)
    for i in range(0, len(split_string_list)):
        f_name = "{}{}.png".format(root, i)
        secret_enc = lsb.hide(f_name, split_string_list[i])
        secret_enc.save(f_name)
        print("[INFO] frame {} holds {}".format(f_name, lsb.reveal(f_name)))


def decode_string(video):
    frame_extraction(video)
    secret = []
    root = "./tmp/"
    for i in range(len(os.listdir(root))):
        f_name = "{}{}.png".format(root, i)
        print("[INFO] frame {} is decoding".format(f_name))
        secret_dec = lsb.reveal(f_name)
        if secret_dec == None:
            break
        secret.append(secret_dec)
    print("[INFO] secret is {}".format("".join(secret)))
    print(''.join([i for i in secret]))
    # clean_tmp()


def clean_tmp(path="./tmp"):
    if os.path.exists(path):
        shutil.rmtree(path)
        print("[INFO] tmp files are cleaned up")


def main():
    input_string = input("Enter the input string: ")
    f_name = input("enter the name of video: ")

    # 从源文件分离出帧
    frame_extraction(f_name)
    
    # 分离文件路径和扩展名
    file_path, file_extraction = os.path.splitext(f_name)
    
    # 创建输出音频文件
    audio_path = file_path + "_temp.mp3"
    video = VideoFileClip(f_name)
    video.audio.write_audiofile(audio_path)

    # 加密字符
    encode_string(input_string)

    # 从tmp文件夹的图片创建没有声音的视频
    fps=30
    img_root = r"./tmp/"
    # fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    video_file_path = file_path + "_temp.avi"
    # 获取tmp文件夹第一张视频的尺寸
    img = cv2.imread(img_root + "0.png")
    height, width, layers = img.shape
    size=(width,height)
    videoWriter = cv2.VideoWriter(video_file_path,fourcc=fourcc,fps=fps,frameSize=size)
    for i in range(len(os.listdir(img_root))):
        frame = cv2.imread(img_root+str(i)+'.png')
        videoWriter.write(frame)
    videoWriter.release()

    # 合并视频和音频     audio_path   video_file_path
    video = VideoFileClip(video_file_path)
    audio_clip = AudioFileClip(audio_path)
    video = video.set_audio(audio_clip)
    video.write_videofile(file_path + "_hide.avi")
    clean_tmp()


if __name__ == "__main__":
    while True:
        print("1.Hide a message in video 2.Reveal the secret from video")
        print("any other value to exit")
        choice = input()
        if choice == '1':
            main()
        elif choice == '2':
            decode_string(input("enter the name of video with extension: "))
        else:
            break

I have tried mp4, avi, wov format. But none of them worked.

IF YOU HAVE ANY IDEA OR SUGGESTION GIVEN TO ME, I WOULD BE VERY GRATEFUL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文