--- layout: post title: "Using spectral analysis to watch a YouTube video" date: 2022-08-29 01:49:02 +0100 --- So I went to watch a video on YouTube: And about a minute in, I noticed some strange high frequency in both audio channels. Here's a sample: [(Signature)][sig2] Not sure what this is, I decided to download it to investigate further: $ youtube-dl -F 'https://www.youtube.com/watch?v=7fSq0_Ljb_s' One of the output lines is: 140 m4a audio only tiny 129k , m4a_dash container, mp4a.40.2@129k (44100Hz), 84.38MiB So: $ youtube-dl -f 140 'https://www.youtube.com/watch?v=7fSq0_Ljb_s' $ ffmpeg -i '【GAMABOOKS_コラボ企画】星空古本市開催!【新人VTuber】-7fSq0_Ljb_s.m4a' -ss 02:00 -t 30 gamabooks_cut.wav $ octave-cli octave:1> [y, Fs] = audioread('gamabooks_cut.wav'); octave:2> Y = fft(y); octave:3> plot(abs(Y)); ![fft spectrum][img0] [(Signature)][sig0] Here's our high frequency. Now zoom in to find the FFT index number: ![fft zoomed][img1] [(Signature)][sig1] The peak is at index 338543. Convert it to hertz (and note the 1-index): octave:4> (338543-1) / length(y) * Fs ans = 1.1285e+04 So now we just need a way to suppress this frequency. Searching "mpv audio filter" online tells us that `mpv --af=help` will list all audio filters available. Looking through the list, I found: bandreject Apply a two-pole Butterworth band-reject filter. So I just need to find out how to configure this filter. Searching online, I eventually came across some scripts that referenced the [FFmpeg manual][eq], specifically, the "equalizer" filter: (emphasis mine) > Apply a two-pole peaking equalisation (EQ) filter. With this filter, the > signal-level at and around a selected frequency can be increased or decreased, > whilst **(unlike bandpass and bandreject filters) that at all other frequencies is unchanged.** Sounds like what I want. So to try it, with reference to the manual (the bandwidth and attenuation factor were determined by trial-and-error): $ mpv --af=equalizer=f=11285:t=h:width=5:g=-100 gamabooks_cut.wav Sounds a lot better. Now download the video: $ youtube-dl -f 136 'https://www.youtube.com/watch?v=7fSq0_Ljb_s' And play it: $ mpv 【GAMABOOKS_コラボ企画】星空古本市開催!【新人VTuber】-7fSq0_Ljb_s.mp4 --audio-file=【GAMABOOKS_コラボ企画】星空古本市開催!【新人VTuber】-7fSq0_Ljb_s.m4a --af=equalizer=f=11285:t=h:width=5:g=-100 Done! [eq]: https://ffmpeg.org/ffmpeg-filters.html#equalizer [img0]: /blog/images/2022-08-29-spectral-analysis-gamabooks-youtube-video.0.png [sig0]: /blog/sig/2022-08-29-spectral-analysis-gamabooks-youtube-video.0.png.sig [img1]: /blog/images/2022-08-29-spectral-analysis-gamabooks-youtube-video.1.png [sig1]: /blog/sig/2022-08-29-spectral-analysis-gamabooks-youtube-video.1.png.sig [sig2]: /blog/sig/2022-08-29-spectral-analysis-gamabooks-youtube-video.2.wav.sig