Skip to content

Instantly share code, notes, and snippets.

@tommy-mor
Last active July 23, 2024 14:43
Show Gist options
  • Save tommy-mor/d362754bf91a95ab59658cbc8da5a39f to your computer and use it in GitHub Desktop.
Save tommy-mor/d362754bf91a95ab59658cbc8da5a39f to your computer and use it in GitHub Desktop.
not good code stay away
from moviepy.editor import *
import numpy as np
clip = VideoFileClip("hams.mkv")
import sounddevice as sd
import soundfile as sf
from queue import Queue
from collections import OrderedDict
import pprint
pp = pprint.PrettyPrinter(indent=4)
print('started')
print('sarted')
def freq(x):
b = x.mean(1)
d = (np.fft.rfft(b)**2)
w = d[1:].argmax() + 1
return w
normal_audioarr = [ clip.audio.get_frame(t/clip.audio.fps) for t in range(0,int(clip.duration*clip.audio.fps))]
#normal_audioarr = [ clip.audio.get_frame(t/clip.audio.fps) for t in range(0,int(5*clip.audio.fps))]
video_clips = [clip.get_frame(t/clip.fps) for t in range(0,int(clip.fps*clip.duration))]
chunked = enumerate(np.array_split(np.array(normal_audioarr), int(clip.duration) * 15))
chunked_vid = dict(enumerate(np.array_split(video_clips, int(clip.duration) * 15)))
#weird_audio = sorted(chunked, key=lambda x: abs(x[1]).sum())
weird_audio = sorted(chunked, key=lambda x: freq(x[1]))
weird_audio_dict = OrderedDict(weird_audio)
weird_vid = [chunked_vid[i] for i in weird_audio_dict.keys()]
print('concating audio')
data = list(map(lambda x: x[1], weird_audio))
l = np.array(data[0])
for i in range(1,len(data)):
l = np.append(l, data[i], 0)
print(l.shape)
print(len(l))
data = l
#data = normal_audioarr
#aclip = AudioClip(make_frame=make_frame, duration=clip.duration)
#aclip.
fs = 44100
print("playing")
# NOTE, we will probably have to do it like 4 frames at a time so that it sounds like not static
sf.write('myfile.wav', data, fs)
print('concating video')
l = np.array(weird_vid[0])
for i in range(1,len(weird_vid)):
l = np.append(l, weird_vid[i], 0)
print(l.shape)
global it
it = 0
def make_frame(t):
global it
it += 1
if it >= 4846:
return l[5]
return l[it]
vid = VideoClip(make_frame, duration = clip.duration)
audio = AudioFileClip('myfile.wav')
vid.set_audio(audio)
vid.write_videofile('out.mp4',clip.fps)
print('done')
#b has the suond data assoc with its frame
# sort b, then give each of b its frame using get_frame maybe?
#then make an audio clip using audioarrayclip which doesent exist yet
#then profit
#audioclip.preview()
@mrchantey
Copy link

68 lines of genius

@SkleKng
Copy link

SkleKng commented Feb 22, 2023

this is the funneist thing I've ever read

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment