-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshuffle_video.py
More file actions
101 lines (84 loc) · 3.67 KB
/
shuffle_video.py
File metadata and controls
101 lines (84 loc) · 3.67 KB
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
import os
import random
import tempfile
import shutil
import cv2
import moviepy.editor as mp
import numpy as np
from pydub import AudioSegment
def shuffle_video(input_file, output_file):
# Create a unique temporary directory
temp_dir = tempfile.mkdtemp()
try:
# Temporary directories for frames and audio snippets
frames_dir = os.path.join(temp_dir, "frames")
audio_dir = os.path.join(temp_dir, "audio")
os.makedirs(frames_dir)
os.makedirs(audio_dir)
# Extract frames from the video
video_cap = cv2.VideoCapture(input_file)
fps = video_cap.get(cv2.CAP_PROP_FPS)
frame_count = int(video_cap.get(cv2.CAP_PROP_FRAME_COUNT))
print("Extracting frames...")
frame_paths = []
for i in range(frame_count):
ret, frame = video_cap.read()
if not ret:
break
frame_path = os.path.join(frames_dir, f"frame_{i:04d}.png")
cv2.imwrite(frame_path, frame)
frame_paths.append(frame_path)
video_cap.release()
# Extract audio
print("Extracting audio...")
video = mp.VideoFileClip(input_file)
audio = AudioSegment.from_file(input_file)
duration = len(audio) # duration in milliseconds
audio_snippets = []
snippet_duration = int(duration / frame_count)
for i in range(frame_count):
start = i * snippet_duration
end = start + snippet_duration
snippet = audio[start:end]
snippet_path = os.path.join(audio_dir, f"audio_{i:04d}.wav")
snippet.export(snippet_path, format="wav")
audio_snippets.append(snippet_path)
# Shuffle frames and audio together
print("Shuffling frames and audio...")
indices = list(range(frame_count))
random.shuffle(indices)
shuffled_frames = [frame_paths[i] for i in indices]
shuffled_audio = [audio_snippets[i] for i in indices]
# Reassemble shuffled frames into a video
print("Creating shuffled video...")
height, width, _ = cv2.imread(shuffled_frames[0]).shape
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
shuffled_video_path = os.path.join(temp_dir, "shuffled_video.mp4")
video_writer = cv2.VideoWriter(shuffled_video_path, fourcc, fps, (width, height))
for frame_path in shuffled_frames:
frame = cv2.imread(frame_path)
video_writer.write(frame)
video_writer.release()
# Reassemble shuffled audio
print("Creating shuffled audio...")
shuffled_audio_combined = AudioSegment.empty()
for snippet_path in shuffled_audio:
snippet = AudioSegment.from_file(snippet_path)
shuffled_audio_combined += snippet
shuffled_audio_path = os.path.join(temp_dir, "shuffled_audio.wav")
shuffled_audio_combined.export(shuffled_audio_path, format="wav")
# Combine shuffled video and audio
print("Combining shuffled video and audio...")
shuffled_video = mp.VideoFileClip(shuffled_video_path)
shuffled_video = shuffled_video.set_audio(mp.AudioFileClip(shuffled_audio_path))
shuffled_video.write_videofile(output_file, codec="libx264", audio_codec="aac")
print(f"Shuffled video saved as: {output_file}")
finally:
# Cleanup temporary directory
shutil.rmtree(temp_dir)
print("Temporary files cleaned up.")
# Example usage
if __name__ == "__main__":
input_path = input("Enter the path to the input video file: ").strip()
output_path = input("Enter the desired name for the output video file: ").strip()
shuffle_video(input_path, output_path)