mirror of
https://github.com/osmarks/random-stuff
synced 2025-02-03 20:59:17 +00:00
misc python
This commit is contained in:
parent
c6a66dbb8b
commit
bb27d89936
47
alexandergriffing_spite.py
Normal file
47
alexandergriffing_spite.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
from decimal import Decimal
|
||||||
|
import itertools
|
||||||
|
|
||||||
|
def sum_series(series):
|
||||||
|
x = Decimal(0)
|
||||||
|
i = 0
|
||||||
|
t = 0
|
||||||
|
l = 0
|
||||||
|
# ethical
|
||||||
|
for value in series:
|
||||||
|
last_x = x
|
||||||
|
x += value
|
||||||
|
if x == last_x:
|
||||||
|
l += 1
|
||||||
|
else:
|
||||||
|
t = i
|
||||||
|
l = 0
|
||||||
|
if l > (i // 2 + 2): return x
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
def power_series(x, coefs):
|
||||||
|
def f():
|
||||||
|
a = Decimal(1)
|
||||||
|
for coef in coefs:
|
||||||
|
yield a * coef
|
||||||
|
a *= x
|
||||||
|
return sum_series(f())
|
||||||
|
|
||||||
|
def derivative(power_series):
|
||||||
|
next(power_series)
|
||||||
|
for value, i in zip(power_series, itertools.count(1)):
|
||||||
|
yield i * value
|
||||||
|
|
||||||
|
def cos():
|
||||||
|
for i in itertools.count(0):
|
||||||
|
yield Decimal(-1)**i / Decimal(math.factorial(i * 2))
|
||||||
|
yield 0
|
||||||
|
|
||||||
|
def newton(func, x0):
|
||||||
|
x = x0
|
||||||
|
while True:
|
||||||
|
fx, fprimex = power_series(x, func()), power_series(x, derivative(func()))
|
||||||
|
x = x - fx / fprimex
|
||||||
|
yield x
|
||||||
|
|
||||||
|
for half_pi in newton(cos, 1):
|
||||||
|
print(half_pi * 2)
|
93
lesswrong_to_tiktok.py
Normal file
93
lesswrong_to_tiktok.py
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
import wave, sys
|
||||||
|
import nltk
|
||||||
|
from balacoon_tts import TTS
|
||||||
|
from collections import namedtuple
|
||||||
|
import struct
|
||||||
|
from PIL import Image, ImageDraw
|
||||||
|
import cv2
|
||||||
|
import numpy
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
WIDTH = 400
|
||||||
|
|
||||||
|
AUDIO = "/tmp/x.wav"
|
||||||
|
VIDEO = "/tmp/x.avi"
|
||||||
|
BACKDROP = "/tmp/x.mkv"
|
||||||
|
OUTPUT = "/tmp/x.mp4"
|
||||||
|
|
||||||
|
def render_text(text: str):
|
||||||
|
render_params = {"font_size": 24}
|
||||||
|
im = Image.new("RGBA", (WIDTH, WIDTH))
|
||||||
|
draw = ImageDraw.Draw(im)
|
||||||
|
loc = [0, 0]
|
||||||
|
toks = text.split()
|
||||||
|
toks.reverse()
|
||||||
|
text_commands = []
|
||||||
|
while toks:
|
||||||
|
chunk = []
|
||||||
|
while draw.textbbox(loc, " ".join(chunk), **render_params)[2] < WIDTH:
|
||||||
|
if not toks: break
|
||||||
|
chunk.append(toks.pop())
|
||||||
|
else: toks.append(chunk.pop())
|
||||||
|
bbox = draw.textbbox(loc, " ".join(chunk), **render_params)
|
||||||
|
text_commands.append((tuple(loc), " ".join(chunk)))
|
||||||
|
loc[1] = bbox[3]
|
||||||
|
draw.rectangle([0, 0, WIDTH, loc[1]], fill="white")
|
||||||
|
for loc, text in text_commands:
|
||||||
|
draw.text(loc, text, fill="black", **render_params)
|
||||||
|
return im
|
||||||
|
|
||||||
|
Pause = namedtuple("Pause", ["length"])
|
||||||
|
|
||||||
|
text = open("/home/osmarks/Downloads/seq1.txt").read()
|
||||||
|
|
||||||
|
tts = TTS("/home/osmarks/Downloads/en_us_hifi_jets_cpu.addon")
|
||||||
|
#supported_speakers = tts.get_speakers()
|
||||||
|
speaker = "6670"
|
||||||
|
|
||||||
|
def chunks(text: str) -> list[str | Pause]:
|
||||||
|
out = []
|
||||||
|
for line in text.splitlines():
|
||||||
|
if line:
|
||||||
|
for sent in nltk.sent_tokenize(line):
|
||||||
|
out.append(sent)
|
||||||
|
out.append(Pause(0.5))
|
||||||
|
out.append(Pause(1))
|
||||||
|
return out
|
||||||
|
|
||||||
|
RATE = tts.get_sampling_rate()
|
||||||
|
FPS = 30
|
||||||
|
|
||||||
|
def wavblank(seconds):
|
||||||
|
return struct.pack(">h", 0) * round(seconds * RATE / 2)
|
||||||
|
|
||||||
|
blank_frame = render_text("")
|
||||||
|
|
||||||
|
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
|
||||||
|
video_writer = cv2.VideoWriter(VIDEO, fourcc, FPS, (WIDTH, WIDTH))
|
||||||
|
total_dur = 0
|
||||||
|
with wave.open(AUDIO, "w") as fp:
|
||||||
|
fp.setparams((1, 2, RATE, 0, "NONE", "NONE"))
|
||||||
|
for chunk in chunks(text):
|
||||||
|
if isinstance(chunk, str):
|
||||||
|
samples = tts.synthesize(chunk, speaker)
|
||||||
|
image = render_text(chunk)
|
||||||
|
elif isinstance(chunk, Pause):
|
||||||
|
samples = wavblank(chunk.length)
|
||||||
|
image = blank_frame
|
||||||
|
fp.writeframes(samples)
|
||||||
|
duration = len(samples) / RATE # what
|
||||||
|
total_dur += duration
|
||||||
|
frame = cv2.cvtColor(numpy.array(image), cv2.COLOR_RGBA2BGR)
|
||||||
|
for _ in range(round(duration * FPS)): video_writer.write(frame)
|
||||||
|
print(chunk, duration)
|
||||||
|
video_writer.release()
|
||||||
|
subprocess.run([
|
||||||
|
"ffmpeg",
|
||||||
|
"-i", BACKDROP, "-i", AUDIO, "-i", VIDEO,
|
||||||
|
"-filter_complex", "overlay=x=200:y=200,format=nv12,hwupload",
|
||||||
|
"-to", str(total_dur),
|
||||||
|
"-y",
|
||||||
|
"-vaapi_device", "/dev/dri/renderD128", "-c:v", "h264_vaapi",
|
||||||
|
OUTPUT
|
||||||
|
]).check_returncode()
|
@ -1,6 +1,6 @@
|
|||||||
import os, sys, subprocess, datetime
|
import os, sys, subprocess, datetime
|
||||||
|
|
||||||
dt_threshold = datetime.datetime(2022, 11, 27).timestamp()
|
dt_threshold = datetime.datetime(2023, 6, 16).timestamp()
|
||||||
|
|
||||||
_, indir, outdir = sys.argv
|
_, indir, outdir = sys.argv
|
||||||
for x in os.listdir(indir):
|
for x in os.listdir(indir):
|
||||||
@ -19,4 +19,4 @@ for x in os.listdir(indir):
|
|||||||
os.rename(inpath, newpath)
|
os.rename(inpath, newpath)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print("keeping")
|
print("keeping")
|
||||||
|
52
rotating_audio.py
Normal file
52
rotating_audio.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import pathlib
|
||||||
|
import numpy as np
|
||||||
|
import wave
|
||||||
|
import math
|
||||||
|
import tempfile
|
||||||
|
import subprocess
|
||||||
|
import contextlib
|
||||||
|
|
||||||
|
cyc = 2
|
||||||
|
nsamples = 48000 * 60
|
||||||
|
path = pathlib.Path("~/Downloads/inputs").expanduser()
|
||||||
|
|
||||||
|
inputs = []
|
||||||
|
tempfiles = []
|
||||||
|
with contextlib.ExitStack() as stack:
|
||||||
|
for file in path.glob("*"):
|
||||||
|
mgr = tempfile.NamedTemporaryFile("w")
|
||||||
|
stack.enter_context(mgr)
|
||||||
|
tempfiles.append(mgr)
|
||||||
|
subprocess.run(["ffmpeg", "-i", file, "-ar", "48000", "-ac", "1", "-filter:a", "dynaudnorm=p=1.0:s=5:g=15", "-y", "-f", "wav", mgr.name])
|
||||||
|
|
||||||
|
for t in tempfiles:
|
||||||
|
with wave.open(str(t.name), "r") as w:
|
||||||
|
assert w.getsampwidth() == 2 and w.getnchannels() == 1 and w.getframerate() == 48000
|
||||||
|
inputs.append(np.frombuffer(w.readframes(nsamples), np.dtype(np.int16).newbyteorder("<")).astype(float) / 32768)
|
||||||
|
|
||||||
|
inputs = np.array(inputs) * (1/len(inputs)) # wrong but close enough
|
||||||
|
offsets = np.broadcast_to(np.linspace(0, math.pi * 2, num=len(inputs) + 1)[:len(inputs)], (nsamples, len(inputs))).transpose()
|
||||||
|
phase = np.broadcast_to(np.linspace(0, math.pi * 2 * cyc, num=nsamples), (len(inputs), nsamples)) + offsets
|
||||||
|
empty = np.zeros_like(phase)
|
||||||
|
x = np.cos(phase)
|
||||||
|
y = np.sin(phase)
|
||||||
|
|
||||||
|
def writeaudio(file, array):
|
||||||
|
wr = wave.open(file, "w")
|
||||||
|
wr.setnchannels(1)
|
||||||
|
wr.setsampwidth(2)
|
||||||
|
wr.setframerate(48000)
|
||||||
|
wr.writeframes((array * 32768).astype("<i2").tobytes())
|
||||||
|
wr.close()
|
||||||
|
|
||||||
|
with tempfile.NamedTemporaryFile("wb") as left:
|
||||||
|
with tempfile.NamedTemporaryFile("wb") as front:
|
||||||
|
with tempfile.NamedTemporaryFile("wb") as right:
|
||||||
|
with tempfile.NamedTemporaryFile("wb") as back:
|
||||||
|
writeaudio(left, sum(np.where(-x > 0, -x, empty) * inputs))
|
||||||
|
writeaudio(right, sum(np.where(x > 0, x, empty) * inputs))
|
||||||
|
writeaudio(front, sum(np.where(y > 0, y, empty) * inputs))
|
||||||
|
writeaudio(back, sum(np.where(-y > 0, -y, empty) * inputs))
|
||||||
|
print(front.name)
|
||||||
|
# layout is technically front left + front right + back left + back right - ignore
|
||||||
|
subprocess.run(["ffmpeg", "-i", front.name, "-i", right.name, "-i", back.name, "-i", left.name, "-filter_complex", "[0:a][1:a]join=inputs=4:channel_layout=quad[a]", "-map", "[a]", "/tmp/out.opus"]).check_returncode()
|
Loading…
Reference in New Issue
Block a user