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()
@ScoreUnder
Copy link

I'm 30 now and have been programming since I was 7. I'm ashamed to say that this is the kind of project I would have spent 2 hours trawling ffmpeg documentation for and then I would have given up.

@tommy-mor
Copy link
Author

@ScoreUnder thanks for kind words. I guess I was particularly bored that day.

@tommy-mor
Copy link
Author

video of this script's output: https://youtu.be/iWFRKZek0FI

@henrahmagix
Copy link

this is so good 😍

@ArjixWasTaken
Copy link

This is so ugly and beautiful at the same time.
This is art.

@vercte
Copy link

vercte commented Feb 19, 2023

This is such legendary shitposting code

@Leonerd-04
Copy link

11 print('sarted')

lmao

@FalconFour
Copy link

FalconFour commented Feb 20, 2023

clip = VideoFileClip("hams.mkv")

Oh ghawd when I watched the video, I guessed when it was built, each frame was exported as a bitmap then reassembled as a video after processing.

when I saw this, all hope was lost

this must have taken 4 years to process

@nothings
Copy link

You should window the FFT, since FFT interprets the input block as being looped infinitely, and it'll interpret the looped signal as having a very sharp discontinuity at the wraparound point, which probably is making the result less accurate.

Also calling the FFT inside sort, whew boy (unless python is memoizing it), but maybe everything else is slow enough it doesn't matter that much.

@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