1 year ago

#375532

test-img

user7025125

Having problem in video stegano, the hiden message always lost

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

python

opencv

ffmpeg

steganography

0 Answers

Your Answer

Accepted video resources