«Function to extract audio from youtube + example usage» by gosub

on 06 Jun'24 08:11 in utilityyoutuberickroll

Helper function to extract audio from a youtube video, and save it to a file.

Note: Linux/OSX/Unix only (uses .unixCmd), needs yt-dlp to be installed, the download procedure is synchronous

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
// Helper function to extract audio from a youtube video
// and save it to a file
// 
// Note:
// - Linux/OSX/Unix only (uses .unixCmd)
// - Needs yt-dlp to be installed
// - The download procedure is synchronous

(
~ytdl = {|vid, filename, debug=false|
	var output;
	if (File.exists(filename)) {
		if (debug) {(filename + "already present").postln};
		filename;
	} {
		if (debug) {("downloading" + vid + "into" + filename).postln};
		output = ["yt-dlp", "--extract-audio", "--audio-format", "wav",
			"--output", filename, vid].unixCmdGetStdOut;
		if (debug) {output.postln};
		filename;
	};
};
)

// example usage
(
~rr = "dQw4w9WgXcQ";
~file = "/tmp/rr.wav";

("downloading:" + ~file).postln;
~ytdl.(~rr, ~file);
("download complete:" + ~file).postln;

~buffer = Buffer.read(s, ~file);
)

(
// BUFFER MUST BE STEREO!
SynthDef(\hellish, {
	|out, bufnum, descent=20, dist=7|
	var beat = 60/113;
	var cuts = [beat, beat/2, beat/3];
	var rates = Line.kr([-1,1,-1], [-1.5, 1.3, -1.2], descent);
	var frames = BufFrames.kr(bufnum);
	var lf = LFNoise0.ar(cuts).linlin(-1, 1, 0, frames);
	var snd = PlayBuf.ar(2, bufnum, BufRateScale.kr(bufnum)/rates,
		Impulse.kr(cuts), startPos: lf);
	snd = Splay.ar(snd.flatten.scramble) * Line.kr(1, dist, descent);
	snd = snd.tanh * 0.5;
	Out.ar(out, snd);
}).add;
)

Synth(\hellish, [\bufnum, ~buffer]);

File.delete(~file);
raw 1414 chars (focus & ctrl+a+c to copy)
reception
comments