Skip to content

An automation process to convert YouTube video into audio file and uploading it to Anchor.fm podcast

License

Notifications You must be signed in to change notification settings

Schroedinger-Hat/youtube-to-spotify

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

Y2S = YouTube to Spotify - An automation tool to publish your podcast

Cover image

This action will upload an audio file from a given YouTube video automatically to your spotify account.

It is very useful in a scenario where you have a YouTube account and also a podcast at Spotify through Anchor.fm.

In our live show (Schrödinger Hat) we had this necessity. So we built it for the open source community.

Every contribution is appreciated, even just a simple feedback.

Table of Contents

How it works

The action will start every time you push a change on the episode.json file. Into this file you need to specify the YouTube id of your video.

The action uses a docker image built over Ubuntu. It takes some time to setup the environment before running the script.

NOTE: For the script to run successfully it is necessary for there to be at least one episode manually published on spotify, as the steps to publish on a brand new spotify account are different, and the automation will break.

How can I run this as a GitHub action?

You can use the latest version of this action from the GitHub Actions marketplace.

In the repository root directory add a episode.json file containing your YouTube video id, e.g.:

{
  "id": "nHCXZC2InAA"
}

Then create a GitHub action in the .github/workflows directory with this yaml:

name: 'Upload Episode from YouTube To Spotify'

on:
  push:
    paths:
      - episode.json
    branches: [main]

jobs:
  upload_episode:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Upload Episode from YouTube To Spotify
        uses: Schroedinger-Hat/youtube-to-spotify@v2.5.0
        env:
          SPOTIFY_EMAIL: ${{ secrets.SPOTIFY_EMAIL }}
          SPOTIFY_PASSWORD: ${{ secrets.SPOTIFY_PASSWORD }}
          EPISODE_PATH: /github/workspace

NOTE: you need to set up the secrets for SPOTIFY_EMAIL and SPOTIFY_PASSWORD. This environment variables are mandatory as they specify the sign in account.

NOTE: Since PR#127 the project remove any reference to Anchor.fm due to the Spotify acquisition of the company (you can read more here).

Environment variables

Login Type

Setting the SPOTIFY_LOGIN to true makes the script login with the old spotify login type. Instead setting it to false makes the script login with the spotify account. By default the value is true.

env:
  SPOTIFY_LOGIN: true

Draft Mode

By setting the SAVE_AS_DRAFT, the new episode will be published as a draft. This can be useful if you need someone else's approval before actual publication.

env:
  SAVE_AS_DRAFT: true

Audio conversion options

ffmpeg is used to convert the video to MP3. It's possible to pass arguments to ffmpeg with POSTPROCESSOR_ARGS environment variable.

See -postprocessor-args syntax and options on https://github.com/yt-dlp/yt-dlp#post-processing-options.

The example below convert the video to mono audio.

env:
  POSTPROCESSOR_ARGS: 'ExtractAudio+ffmpeg:-ac 1'

To convert to mono audio, remove initial silence and apply fade-in:

# remove initial silence quieter than -50dB
env:
  POSTPROCESSOR_ARGS: "ExtractAudio+ffmpeg:-ac 1 -af silenceremove=1:0:-50dB,afade=t=in:d=5"

Explicit Mode

By setting the IS_EXPLICIT, the new episode will be marked as explicit.

env:
  IS_EXPLICIT: true

Sponsored Content

By setting IS_SPONSORED, the new episode will be marked as having promotional content (sponsored). Default is false.

env:
  IS_SPONSORED: true

Thumbnail Mode

By setting the LOAD_THUMBNAIL, the new episode will include the video thumbnail as the episode art.

env:
  LOAD_THUMBNAIL: true

Add YouTube URL to Podcast Description

By setting the URL_IN_DESCRIPTION, the Podcast description will include the YouTube URL on a new line at the end of the description. It is recommended to set it, for if the YouTube video has no description it will fail to save the new episode. Setting it to true guarantees to always have a description.

env:
  URL_IN_DESCRIPTION: true

Set a publish date for the episode

By setting SET_PUBLISH_DATE, the new episode can be scheduled for publishing the episode on the date that the YouTube video is uploaded. Please note that the scheduling will work if SAVE_AS_DRAFT is not set, because spotify doesn't store publish date for draft episodes. If SET_PUBLISH_DATE is not set, then spotify will choose the current date for publishing.

env:
  SET_PUBLISH_DATE: true

Multiple shows per repository

It is possible to use a single repository to maintain several shows.

You'll need an episode config per show.

As an example, suppose you have two shows, you called "Great News" and another "Sad News".

You repository will look like this:

.github/
├─ workflows/
│  ├─ great-news.yaml
│  ├─ sad-news.yaml
great-news.json
sad-news.json

In great-news.json and sad-news.json, you have:

{
  "id": "episode_video_id"
}

In great-news.yaml and sad-news.yaml:

name: 'Great News Upload Action'
on:
  push:
    paths:
    ## only updates to this file trigger this action
      - great-news.json   # or sad-news.json
jobs:
  upload_episode:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Upload Episode from YouTube To Spotify
        uses: Schrodinger-Hat/youtube-to-spotify@v2.5.0
        env:
          SPOTIFY_EMAIL: ${{ secrets.SPOTIFY_EMAIL_GREATNEWS }}  # OR secrets.SPOTIFY_EMAIL_SADNEWS
          SPOTIFY_PASSWORD: ${{ secrets.SPOTIFY_PASSWORD_GREATNEWS }}  # OR secrets.SPOTIFY_PASSWORD_SADNEWS
          EPISODE_PATH: /github/workspace/
          EPISODE_FILE: great-news.json
          # (…) Other configs as needed

How can I setup for development and use the script locally?

To run the script locally, you need python3 and ffmpeg to be available in PATH which are used by the npm dependency youtube-dl-exec.

Clone the repository and run npm ci to install the exact dependencies that are specified in package-lock.json.

After that, you can edit episode.json that is located at the root of this repository. It is recommended to specify the id of a short YouTube video in episode.json for testing.

Then, make sure to setup your .env file in the root of this repository so you can put the environment variables that you normally specify in the GitHub action YAML file.

To do that, you can copy .env.sample into a file with name .env.

Make sure to specify the mandatory environment variables for logging in to spotify, SPOTIFY_EMAIL and SPOTIFY_PASSWORD.

If needed we can set the SPOTIFY_EMAIL and SPOTIFY_PASSWORD too, so they will be used to login with the new login type after changing SPOTIFY_LOGIN to false.

Finally, you can do npm start to execute the script.

How to upload a YouTube playlist to spotify using this script?

⚠ WARNING: This Potentially violates GitHub's Terms of service ⚠

In particular, any repositories that use GitHub Actions or similar 3rd party services solely to interact with 3rd party websites, to engage in incentivized activities, or for general computing purposes may fall foul of the GitHub Additional Product Terms (Actions), or the GitHub Acceptable Use Policies. Actions should not be used for any activity unrelated to the production, testing, deployment, or publication of the software project associated with the repository where GitHub Actions are used.

Currently, you can process a full playlist (one way only) with

curl https://scc-youtube.vercel.app/playlist-items/PLoXdlLuaGN8ShASxcE2A4YuSto3AblDmX \
    | jq '.[].contentDetails.videoId' -r \
    | tac \
    | xargs -I% bash -c "jo id='%' > episode.json && git commit -am % && git push"

https://scc-youtube.vercel.app/playlist-items is from https://github.com/ThatGuySam/youtube-json-server

jo is a json generator https://github.com/jpmens/jo

tac is a command present in most Linux distributions and on mac with brew install coreutils. Its from reversing the list from older to newer. Remove if you want to upload in the order presented on YouTube.

jq is a json processor https://stedolan.github.io/jq/

This must be run on the folder where your episode.json is.

License

MIT