albertyw/albertyw.com

View on GitHub
app/notes/20210716-0510.md

Summary

Maintainability
Test Coverage
Download and Convert Youtube Playlists to MP3 Files

youtube-playlist-mp3

1626412204

These are two scripts to download a youtube playlist of videos, and convert the videos into MP3 files.

youtube-dl supports converting files automatically but requires ffmpeg to be installed on the machine
and visible in `$PATH`.  `convert.sh` instead uses a ffmpeg docker container.

### download.sh

```bash
#!/bin/bash

# Download a youtube playlist

set -euo pipefail
IFS=$'\n\t'

url="$1"

wget https://yt-dl.org/downloads/2021.06.06/youtube-dl-2021.06.06.tar.gz
tar xvf youtube-dl*

screen python3 youtube-dl/youtube-dl "$url"
```

### convert.sh

```bash
#!/bin/bash

# This script converts an mp4 into an mp3
# It works by running ffmpeg in a docker container

set -euo pipefail
IFS=$'\n'

convert () {
    input="$1"
    output="$input.mp3"

    docker run -v "$(pwd):$(pwd)" -w "$(pwd)" jrottenberg/ffmpeg:3.4-scratch \
        -stats \
        -i "./$input" -vn \
        -acodec libmp3lame -ac 2 -qscale:a 4 -ar 48000 \
        "./$output"
}

if [ -z "${1-}" ]; then
    for f in $(find . -type f -name "*.mp4"); do
        convert "$f"
    done
else
    convert "$1"
fi
```