There are times where you might want to convert a YouTube video to MP3, when you just need to listen to the audio only. Maybe you're looking for a podcast or tutorial to keep you company during your commute or workout sessions. Or you are a content creator or musician searching for copyright-free music or a sample for your projects.
This article will walk you through a step-by-step process of how to download YouTube videos and convert them to MP3 audio using the Shotstack Ingest API.
Note: Downloading YouTube videos may be subject to copyright rules and YouTube's terms of service. Be mindful of that when using the solution in this guide. Make sure you aren't violating YouTube's terms or violating any copyright laws.
Shotstack is an automated video creation platform that offers several API products to create, edit, transform and distribute data-driven videos on a large scale.
We will use one of these products, the Ingest API in this tutorial. It's an API you can use to fetch videos from remote URLs and transform in to all kinds of outputs.
Here's what you will need to follow along with this guide:
To download a YouTube video, you need to get a direct link to the video file. This is different from the link to the web page on YouTube that includes comments, related videos, and the video player itself. This URL is not available by default.
Luckily you can use a tool called yt-dlp to get the downloadable video file URL. Simply provide the URL of a YouTube web page and it will work out the exact URL you need to use to download the video.
You can install yt-dlp by following the installation instructions on their GitHub repo here: https://github.com/yt-dlp/yt-dlp/wiki/Installation. Choose the most appropriate option for your operating system or package manager preference.
Once installed and set up you should be able to run the command:
yt-dlp --version
The response should be the date of the latest build, like: 2024-04-09
.
For this tutorial, we'll use the Back End Developer Roadmap 2024 video from freeCodeCamp.org:
Copy the YouTube web page URL from your browser and then run the following command to retrieve the download link:
yt-dlp -f b --get-url https://www.youtube.com/watch?v=tN6oJu2DqCM
The command should return the complete downloadable URL and will look something like this:
https://rr2---sn-huobi-5jjz.googlevideo.com/videoplayback?expire=1711575286&ei=ljwEZr-7LZanp-oPt4u6oAE&ip=154.160.4.247&id=o-AIoPVJYMvycaghaE9sxe1t-Q9BbKBLfls41Z62gNHnx_&itag=22&source=youtube&requiressl=yes&xpc=EgVo2aDSNQ%3D%3D&mh=cO&mm=31%2C29&mn=sn-huobi-5jjz%2Csn-avn7ln7l&ms=au%2Crdu&mv=m&mvi=2&pl=24&initcwndbps=227500&vprv=1&svpuc=1&mime=video%2Fmp4&cnr=14&ratebypass=yes&dur=629.492&lmt=1710208256536185&mt=1711553342&fvip=3&fexp=51141541&c=ANDROID&txp=5532434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cxpc%2Cvprv%2Csvpuc%2Cmime%2Ccnr%2Cratebypass%2Cdur%2Clmt&sig=AJfQdSswRAIgdwH1dly1k34xmFFYxQ_H2OE0DUyMas67Eh_BcNTgZ-MCIBj_qVnNlGZLjNblgHK4US7CMYZN8Wag0WDwgPhz4AfX&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=ALClDIEwRQIgX6_rVQxFFIKHd_ZJ0gSOMoTBvA3PdpNozIVME431zHYCIQDW-nOqaAnRliiiYG5NEOqYP_aKoF7pr1b3w_A23ptWQQ%3D%3D
The URL is lengthy because of the query parameters. These are necessary for security measures and other important settings. Without them, the server may not properly process your request and serve the video file to you. Note that the URL above will have expired by the time you read this so you will need to run the command yourself.
You can test the video file by copying and pasting the URL in to your browser and the video should download and play, without having to visit YouTube.
You now have the direct URL of the YouTube video file. You can now make a POST request to the Ingest API using the URL and convert the video to MP3.
Run the command below in your terminal. Make sure to replace YOUR_API_KEY
with your Shotstack API key. Replace YOUTUBE_VIDEO_URL
with the actual URL you generated from the previous section.
curl -L 'https://api.shotstack.io/ingest/stage/sources' \
-H 'Content-Type: application/json' \
-H 'x-api-key: YOUR_API_KEY' \
-d '{
"url": "YOUTUBE_VIDEO_URL",
"outputs": {
"renditions": [
{
"format": "mp3",
"speed": {
"speed": 1,
"preservePitch": true
}
}
]
}
}'
The command sends a POST request to the Ingest API with a JSON payload that does the following:
It is possible to speed up the audio if you want to absorb information more quickly by adjusting the speed
and preservePitch
options. In this example the values could be omitted as we are not adjusting the speed.
If the request is successful, you should see an output like the following:
{
"data": {
"type": "source",
"id": "zzy8at4g-2gjx-uc2x-euzd-18aayh0tf1og"
}
}
Copy the id
from the response. We will use it to check the status of the audio file generation in the next step.
Shotstack fetches and stores the video before converting it to an MP3 file. It might take several seconds for this process to complete.
Run the command below to check the status of the file. Replace YOUR_API_KEY
with your Shotstack API key, and {{ ID }}
with the id
from the response in the previous step.
curl -X GET https://api.shotstack.io/ingest/stage/sources/{{ ID }} \
-H 'Accept: application/json' \
-H 'x-api-key: YOUR_API_KEY'
If the request succeeds, the response should include relevant details about the source file, and the status of the conversion, like this:
{
"data": {
"type": "source",
"id": "zzy8at4g-2gjx-uc2x-euzd-18aayh0tf1og",
"attributes": {
"id": "zzy8at4g-2gjx-uc2x-euzd-18aayh0tf1og",
"owner": "t2siieowih",
"input": "https://rr2---sn-huobi-5jjz.googlevideo.com/videoplayback?expire=1711575286&ei=ljwEZr-7LZanp-oPt4u6oAE&ip=154.160.4.247&id=o-AIoPVJYMvycaghaE9sxe1t-Q9BbKBLfls41Z62gNHnx_&itag=22&source=youtube&requiressl=yes&xpc=EgVo2aDSNQ%3D%3D&mh=cO&mm=31%2C29&mn=sn-huobi-5jjz%2Csn-avn7ln7l&ms=au%2Crdu&mv=m&mvi=2&pl=24&initcwndbps=227500&vprv=1&svpuc=1&mime=video%2Fmp4&cnr=14&ratebypass=yes&dur=629.492&lmt=1710208256536185&mt=1711553342&fvip=3&fexp=51141541&c=ANDROID&txp=5532434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cxpc%2Cvprv%2Csvpuc%2Cmime%2Ccnr%2Cratebypass%2Cdur%2Clmt&sig=AJfQdSswRAIgdwH1dly1k34xmFFYxQ_H2OE0DUyMas67Eh_BcNTgZ-MCIBj_qVnNlGZLjNblgHK4US7CMYZN8Wag0WDwgPhz4AfX&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=ALClDIEwRQIgX6_rVQxFFIKHd_ZJ0gSOMoTBvA3PdpNozIVME431zHYCIQDW-nOqaAnRliiiYG5NEOqYP_aKoF7pr1b3w_A23ptWQQ%3D%3D",
"source": "https://shotstack-ingest-api-stage-sources.s3.ap-southeast-2.amazonaws.com/t2siieowih/zzy8at4g-2gjx-uc2x-euzd-18aayh0tf1og/source.mp4",
"status": "ready",
"outputs": {
"renditions": [
{
"id": "zzy8at4f-3zkf-wg1z-fgdh-2jhlrt1eqvcf",
"status": "ready",
"url": "https://shotstack-ingest-api-stage-renditions.s3.ap-southeast-2.amazonaws.com/t2siieowih/zzy8at4g-2gjx-uc2x-euzd-18aayh0tf1og/zzy8at4f-3zkf-wg1z-fgdh-2jhlrt1eqvcf.mp3",
"executionTime": 218003.38,
"transformation": {
"format": "mp3",
"speed": {
"speed": 1,
"preservePitch": true
}
},
"duration": 629.39
}
]
},
"width": 1280,
"height": 720,
"duration": 629.43,
"fps": 29.97,
"created": "2024-03-27T17:19:15.669Z",
"updated": "2024-03-27T17:19:23.806Z"
}
}
}
If the video is still being processed, you may see processing
or queued
in the status
parameter of the rendition. Retry the same GET request every few seconds until the value of the status
parameter is ready
.
When the status is ready
, the response will include the full details of the rendition including a url
parameter. Visit the URL contained in this parameter to access the MP3 file, which you can play in your browser or download.
The value of the url
parameter will be something like this:
https://shotstack-ingest-api-stage-renditions.s3.ap-southeast-2.amazonaws.com/t2siieowih/zzy8at4g-2gjx-uc2x-euzd-18aayh0tf1og/zzy8at4f-3zkf-wg1z-fgdh-2jhlrt1eqvcf.mp3
You can also run a command, like wget
to download it to your computer:
wget https://shotstack-ingest-api-stage-renditions.s3.ap-southeast-2.amazonaws.com/t2siieowih/zzy8at4g-2gjx-uc2x-euzd-18aayh0tf1og/zzy8at4f-3zkf-wg1z-fgdh-2jhlrt1eqvcf.mp3
And here is the final MP3 file converted from the YouTube video:
In this article, you have learned how to download and convert YouTube videos to MP3 audio files. We did this using the open source yt-dlp library and the Shotstack Ingest API service. Everything was done using the command line which lends itself to automating the process using a script or building an easy to use interface. Make sure you adhere to YouTube's terms of service and any applicable copyright laws if you build any kind of commercial application using any of these tools.
Every month we share articles like this one to keep you up to speed with automated video editing.