Skip to content

Instantly share code, notes, and snippets.

@Alexwi1son
Forked from flowernert/flo-dl.md
Created March 18, 2025 08:19
Show Gist options
  • Save Alexwi1son/30877992683009895e982b6f063b7db0 to your computer and use it in GitHub Desktop.
Save Alexwi1son/30877992683009895e982b6f063b7db0 to your computer and use it in GitHub Desktop.
Youtube video to audio mp3

Youtube to MP3 downloader/converter

Prototype to be able to export the audio from a Youtube video and make it available to my smartphone.

Use case : keeping video as offline podcasts I can listen in the metro when there's no cell network available.

Linux only.

Uses the excellent https://github.com/ytdl-org/youtube-dl

If enough people have an interest, I might wrap this in something less artisanal (still only on Linux though).

Concept

Create a new XDG url-handling protocol called flodl. Embed a javascript call to this protocol in a browser bookmark, which pass the YT video URL as parameter to the call. The call open a custom script making use of youtube-dl project to download the sound and convert it to mp3. The script finally exports the mp3 file to my synchronized dropbox folder.

Readings on used concepts

Primary readings

XDG protocol creation

Documentation of youtube dl FOSS project

A free non OSS project which gave me the idea for the use of XDG URL-handling protocols and from which I reused the bookmark calling javascript code idea

Additional readings

http://edoceo.com/howto/xfce-custom-uri-handler

How-to

1. Desktop entry

Put the following content in a file in /usr/local/share/applications/flodl.desktop (or ~/.local/share/applications/flodl.desktop)

[Desktop Entry]
Name=Flo DL
Comment=Launch the mp3 dl script
Exec=/home/flo/bin/youtube-dl/to_mp3.sh %u
Type=Application
Categories=Network;FileTransfer;
StartupNotify=true
MimeType=x-scheme-handler/flodl;

2. Register the scheme

In a terminal

xdg-mime default flodl.desktop x-scheme-handler/flodl

sudo update-desktop-database

3. Install the youtube dl python script

In a terminal

sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o youtube-dl.sh

sudo chmod a+rx /usr/local/bin/youtube-dl

4. Custom download and convert script

Put the following content in $HOME/bin/youtube-dl/to_mp3.sh and chmod +x it

#!/bin/zsh

a="$(echo $1 | awk -F'://|&' '{print $2}')"

notify-send "downloading $a"

tmp_dir=out_$(date '+%s')
mkdir $tmp_dir

$HOME/bin/youtube-dl/youtube-dl.sh -f bestaudio $a -x --audio-format mp3 --audio-quality 128k -o "./$tmp_dir/%(uploader)s - %(title)s.%(ext)s"

cp -r ./$tmp_dir/* "$HOME/Dropbox/Audio YT/"
rm -rf ./$tmp_dir

notify-send "Done"

5. Calling the script from a playing video

In your browser, create a new bookmark, and use

javascript:if(navigator.userAgent.indexOf('Safari')%20%3E=%200){s=getSelection();}else{s=document.selection?document.selection.createRange().text:document.getSelection();}document.location.replace('flodl://'+(location.href).replace(/https?:\/\//i,''));

in the address field.

(optional) In your browser you might have to associate the flodl links to calling the system app handling flodl links (being: the script described at step 4.). I didn't have to in my firefox browser but your mileage may vary.

6. Fun and profit

Just hit the bookmark whenever playing a youtube video in the active tab, the audio will be downloaded, converted to mp3 and placed to the Dropbox synchronized folder

This code is licensed CC-BY-NC-SA Licensed CC-BY-NC-SA

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