-
Notifications
You must be signed in to change notification settings - Fork 18
/
inference_av.py
144 lines (114 loc) · 4.67 KB
/
inference_av.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import argparse
import torch
import torchaudio
import torchvision
from audioseal import AudioSeal
import videoseal
from videoseal.utils.display import save_video_audio_to_mp4
parser = argparse.ArgumentParser(description="Video and Audio Watermarking")
parser.add_argument(
"--input_file", type=str, required=True, help="Path to the input mp4 file"
)
parser.add_argument(
"--output_file",
type=str,
required=False,
default="watermarked.mp4",
help="Path to save the watermarked mp4 file",
)
parser.add_argument(
"--video_only", action="store_true", help="Watermark only the video, not the audio"
)
parser.add_argument(
"--detect",
action="store_true",
help="Detect watermarks in the output video and audio",
)
args = parser.parse_args()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load the VideoSeal model
video_model = videoseal.load("videoseal")
video_model.eval()
video_model.to(device)
# Read the video and convert to tensor format
video, audio, info = torchvision.io.read_video(args.input_file, output_format="TCHW")
assert "audio_fps" in info, "The input video must contain an audio track. Simply refer to the main videoseal inference code if not."
fps = info["video_fps"]
sample_rate = info["audio_fps"]
# Normalize the video frames to the range [0, 1] and trim to 1 second
audio = audio.float()
video = video.float() / 255.0
if not args.detect:
# Perform watermark embedding on video
with torch.no_grad():
outputs = video_model.embed(video, is_video=True)
# Extract the results
video_w = outputs["imgs_w"] # Watermarked video frames
video_msgs = outputs["msgs"] # Watermark messages
if not args.video_only:
# Resample the audio to 16kHz for watermarking
audio_16k = torchaudio.transforms.Resample(sample_rate, 16000)(audio)
# If the audio has more than one channel, average all channels to 1 channel
if audio_16k.shape[0] > 1:
audio_16k_mono = torch.mean(audio_16k, dim=0, keepdim=True)
else:
audio_16k_mono = audio_16k
# Add batch dimension to the audio tensor
audio_16k_mono_batched = audio_16k_mono.unsqueeze(0)
# Load the AudioSeal model
audio_model = AudioSeal.load_generator("audioseal_wm_16bits")
# Get the watermark for the audio
with torch.no_grad():
audio_msg = torch.randint(
0,
2,
(audio_16k_mono_batched.shape[0], audio_model.msg_processor.nbits),
device=audio_16k_mono_batched.device,
)
watermark = audio_model.get_watermark(
audio_16k_mono_batched, 16000, message=audio_msg
)
# Embed the watermark in the audio
audio_16k_w = audio_16k_mono_batched + watermark
# Remove batch dimension from the watermarked audio tensor
audio_16k_w = audio_16k_w.squeeze(0)
# If the original audio had more than one channel, duplicate the watermarked audio to all channels
if audio_16k.shape[0] > 1:
audio_16k_w = audio_16k_w.repeat(audio_16k.shape[0], 1)
# Resample the watermarked audio back to the original sample rate
audio_w = torchaudio.transforms.Resample(16000, sample_rate)(audio_16k_w)
else:
audio_w = audio
# Save the watermarked video and audio
save_video_audio_to_mp4(
video_tensor=video,
audio_tensor=audio_w,
fps=int(fps),
audio_sample_rate=int(sample_rate),
output_filename=args.output_file,
)
print(f"encoded message: \n Audio: {audio_msg} \n Video {video_msgs[0]}")
else:
# Detect watermarks in the video
with torch.no_grad():
msg_extracted = video_model.extract_message(video)
print(f"Extracted message from video: {msg_extracted}")
if not args.video_only:
if len(audio.shape) == 2:
audio = audio.unsqueeze(0) # batchify
# if stereo convert to mono
if audio.shape[1] > 1:
audio = torch.mean(audio, dim=1, keepdim=True)
# Load the AudioSeal detector model
detector = AudioSeal.load_detector("audioseal_detector_16bits")
# Detect watermarks in the audio
with torch.no_grad():
result, message = detector.detect_watermark(
torchaudio.transforms.Resample(sample_rate, 16000)(audio), 16000
)
print(f"Detection result for audio: {result}")
print(f"Extracted message from audio: {message}")