July 202529:1513.1k views

Downloading YouTube videos

About

I automate the entire process of turning YouTube videos into podcast episodes using Laravel, yt-dlp, and the Transistor.fm API. From fetching videos to extracting audio and publishing drafts, we automate it all. Whether you're a Laravel dev or just love seeing behind-the-scenes automation, you're in for a fun and practical walkthrough.

Transcript

Everything I said

00:00

I'm constantly on a quest to automate as much as I can. I'm running a business here, and there's a bunch of stuff that I have to do that I don't wanna do, and frankly, eh, I shouldn't really be doing. So today, what we're gonna automate is taking YouTube videos off of YouTube. These are my videos. Don't call the police. We're gonna take YouTube videos off of there.

00:20

We're gonna strip out the audio, and then we are going to publish that audio as a podcast on Transistor FM. If you are not familiar with Transistor FM, you can host unlimited shows over there. You can publish it to all the different places. Um, I have been using Transistor for many years now. You'll see some very old podcasts in here and some very new ones.

00:43

So Mostly Technical is a podcast I run with my friend Ian, and the one that we're gonna be working on today is Database School. So Database School comes from this YouTube playlist. So you can see we've got a bunch of videos. I do these video interviews, and then, uh, a lot of people wanna listen on audio, and frankly, eh, I can't blame 'em.

01:04

I mean, what are you looking at me for? So if you go to Database School, uh, I've actually got the real domain now, so we can go to databaseschool.com. This is a Transistor FM website. I didn't do any of this, but this is the website that Transistor has created for me so people can, uh, listen online, or more importantly, they can listen via RSS. That's the whole goal, is to get this RSS feed into people's podcatchers.

01:28

So this is what we're gonna be working on today. We're gonna be using, uh, the YouTube API, uh, yt-dlp, which has some FFmpeg in it, and then the Transistor API. All right. That's what we're gonna build, but before we do, a quick pitch for Transistor. I reached out to the founder of Transistor and said, "I'm doing this video. Do you want to sponsor?" And he said yes, and that's, that makes me feel really great.

01:52

I've been using Transistor for five or so years. That's where I put all of my shows, 'cause you can put unlimited shows over there. They'll submit it to Apple and Spotify and Overcast and Downcast. They'll submit it to all those places for you, but the support is human beings, actual human beings that will read your email and respond to the question that you asked.

02:13

So it's very helpful. I highly recommend you check out Transistor. Now, let's get into some code. Here, we have an empty Laravel command, and that's what we're gonna be using. You can use any language, any framework you want. I like Laravel, and we've also pulled in this YouTube package, which gives us a nice interface to the YouTube API. For Transistor, we actually don't need a package at all. Their API is very good, very standard, and we're just gonna use Laravel's HTTP facade.

02:38

It's actually very easy. So coming back over here, first thing we need to do is get all the videos, and we're gonna use this getPlaylistItemsByPlaylistId, and that's that playlist ID that I showed you earlier. Now, if we just see what we are working with, we will run php artisan sync, and we get a bunch of stuff.

03:00

Okay, cool. So that looks like it's working. I see DHH Discusses SQLite, which is the first video on the playlist, and if we go all the way back to the top, here we go, um, it looks like it's nested in results, and then it's just an array.

03:22

Cool. That is good. So we are going to collect videos, results. Now, what I'm after is I'm actually after the video's unique ID. Every YouTube video, uh, gets a unique ID. That's how we're gonna dedupe this stuff. So if we look at... I don't think that is right. That's not the right ID.

03:43

That's, like, a playlist item ID. What I want is somewhere resource ID, video ID. That seems right to me. So we're gonna collect that, and then we're going to map over it, and if we... Can we do... I don't know what type this is. Look at me, talking about types.

04:04

Uh, it's just a standard object. So we will try snippet. Um, I don't think it was snippet title. It was snippet resource ID. Snippet. Let's just see what the snippet is to see if we're close. Okay. Snippet, we are close.

04:26

Then we want resource ID. Yep, and then we want video ID. Got it. All right. So we can, we could pluck this, but we're just gonna, we're just gonna return that for now, and then we'll see if the whole collection is there. Got it. Okay. Those are the 17 videos that are on the YouTube playlist right now.

04:52

Now we gotta figure out, how do we talk to Transistor and find out if any of those videos are not on Transistor? We're gonna put you on ice for just a second while we build up the Transistor side. So we're gonna say http base_url. That's not quite right. What is the base URL? Let's check the docs.

05:14

Uh, it's a JSON API API, which is good. That means everything will be exactly as you expect. Now we need a Transistor, transistor.fm/. There we go. It looks like all of them are to v1. All right, cool. So we will take v1 as the base, and then the API key, I believe, is X-API...

05:39

Oh, it's right there, X-API-Key. Cool. So we'll do withHeader X-API-Key. Boy, it really wants to be YouTube, doesn't it? And no, we're not gonna pull it out of the env. That's silly. We're gonna do services transistor, and I think it's just services transistor. Yeah, there's my Transistor key. The reason we don't pull it out of the ENV anywhere besides the config files is once the, uh, ENV or the config files are cached, it won't go back to the ENV.

06:08

So if you do this in production, you need to make sure that you're only referencing ENV in config files. There's a little freebie for you. Okay, now we gotta figure out what episodes, rather what videos are already on Transistor. I imagine, um, let's just do this. Let's do client get, um...

06:30

I don't think we can do episodes yet, 'cause we gotta figure out what show we have, uh, 'cause it has a show ID. So we'll do get shows, and we'll call this shows. We'll throw if there's an error. We'll take JSON. And I think everything is wrapped in data, and we just wanna unwrap that. So then we will see what the shows are, and AR sync should do it.

06:56

There we go. All right. Uh, Aaron and Colleen, that's an old show. Hammerstone. There's the ID. That is what we're after. So I want database school and 54476. We will say that show ID equals 54476. We don't need you anymore.

07:18

Now I want episodes. Episodes, client get episodes. And I bet we have to pass through a... This is just guessing, and I imagine because it is a normal JSON API that this will... There's shows. Nope, I wanted episodes. And got it.

07:40

Got it. Okay. Um, and let's just DD those episodes and see what the shape of those are. We could read the docs, which I love, but, man, let's just figure it out together. Okay. Uh, we have ID attributes. The test, great. That's what I'm after. That's the first one, or rather the most recent one.

08:02

And this looks useful to me. That's what I'm after right there. I need that video ID. So it's in attributes video URL. Let us, uh, collect these, and we'll collect the episodes. Don't wanna each over them. I, in fact, I wanna map over them. And I don't need you.

08:25

Episode attributes, and I think it was called video URL. Let's see how close that got us. Cool. Now I need to, we'll just say URL equals this. We might clean this up in a second, but I'm happy so far. Parse URL, URL. Uh, I just want the query.

08:48

Okay, so the query equals that. Oh, I didn't do anything with it. Query equals that, and cool. Got it. Now we're gonna use a lesser known parse string. Um, no, don't change it. I want to parse string of the query, and I'm gonna put this into params.

09:09

So now I think that should do the separation for us. Yeah, cool. That's pretty cool, right? Parse string, it takes a query string and it puts it into an associative array. Huh. Good job, PHP. In fact, let's just return the params and we can go back, we can go back to Laravel's collection, and we can say pluck the V out and, hey, we

09:41

did it. We got a bunch of YouTube IDs. That's kinda cool. So now we have two lists. What are we gonna do with two lists? We're going to compare them to see if there are any videos on YouTube that are not on Transistor. I don't care about the other way around. If I end up with episodes on Transistor that aren't on YouTube, I don't know that, how that happened.

10:02

But when there are new videos, we need to make new audio episodes. Let's do that. Let's come back up here and we're gonna, we're gonna turn this back on 'cause we're back in YouTube land. And one of the things that is very important about this is I don't wanna enter information twice. So instead of just returning the video ID, what I'm gonna return here is the video ID, but also the title and description, 'cause we already did such a good job of writing it for YouTube that I don't want, you know, I don't wanna do it again.

10:31

And that, that defeats the whole purpose. The purpose is to make my life easier, not to make work still equally hard, but get to use programming also. All right, cut that out, bring it down here. So let's see. Um, we're gonna get all of the videos from Transistor, and we'll name these, uh, existing, the existing ones.

10:57

Um, and we're gonna to array it at the end and drop off that. And goodness gracious, we gotta make this a little prettier. Have some pride. Okay. Now, here are the YouTube videos, and we'll call these, uh, videos actually is, is pretty, it's pretty reasonable. Now let's do this.

11:19

Let's reject any video where existing. So if we come in here and we do a DD of video, we are going to see this array shape right here. So if we run this again, we'll see, yep, that array.

11:40

Cool. We got the good stuff. We got the video ID, which obviously matters, and the title and description. So what we can do is use existing. We can return in array. The needle is the video, video ID, and the haystack is existing. So if there are any, if this ID exists on Transistor already, we don't want it Hey, look at that.

12:09

Not terribly interesting, but this means we're up to date. Now, if we come over here, and sorry if you were relying on this, but I'm just gonna add an XX to the live show because you know what? We'll do it live. So now, look. Hey, look at that.

12:31

Look, look, look. It says, "VITESS for PostgreSQL," uh, "is no longer on Transistor." And the reason is because I mucked up that, that video ID, and so the logic is saying, "Hey, this ID doesn't exist on Transistor." And in fact, it doesn't, because I mucked it up and put the little XX at the end, XX. So we're in a good spot.

12:53

Now we have our... We can call this the, um, remaining. Um, we can call this to publish. Nah, I like the one word, remaining. So the remaining videos are that and okay.

13:14

We're in a good spot. Now we know which videos need to go on Transistor. We're gonna use yt-dlp, which is a nice little tool that allows you to download YouTube videos, YouTube videos that you're legally entitled to, and I'm legally entitled to these YouTube videos, so I'm gonna download them. The first thing is I'm gonna make up a file name. Actually, huh, I lied. The first thing is, if remaining is empty, I am going to say, "No new episodes." Okay?

13:45

Now we can get past this little guard, and frankly, there should only be one new episode at a time. I never, I never do a double feature. I never new, uh, never do a, a back-to-back release. So we're gonna just call this down, and we'll just overwrite the variable. It's my life. Remaining equals remaining first.

14:07

This should now be an array, so an array of a single episode. Yep, that is what I want. Now, I'm gonna create a file name. That's good enough. I, I just need any random file name will work, and the path is I wanna put it in, uh, that is close.

14:28

I like to put-- I like to do this sometimes, um, and then have a process that comes back and cleans up temp files that are greater than seven days old. That way, I don't have to write a cleanup in every single command that is using this temp storage. Eh, just put it in temp, and then if it's older than seven days, you're gone. All right.

14:49

Now we need to actually, you know, run yt-dlp, and so we're gonna use Laravel's process facade, and we're gonna set a super long timeout. We're gonna set a timeout of twenty minutes because it could take a long time. So we'll say start, and the-- we're just gonna pass it as a string, and we're gonna start here, yt-dlp, a YouTube video.

15:12

Now, fortunately, we've got remaining video ID. In fact, let's bring-- While we're working, let's bring this up here, and that is looking right so far. Now, we have a couple of options here, and I think Copilot has gotten it right.

15:38

Um, yeah, let's see if that is actually right. So I know that X is extract audio, and the audio format is MP3, and the output is path. So frankly, I think that got that exactly right. If we do sync again, we'll see. Yeah, and then, uh, while we're out here, let's just run it. Let's just run it and see what happens.

15:59

So which yt-dlp do I... Good, I do have that from Homebrew. So if I run that, cool. It looks like it's trying a bunch of different formats, and there we go. So it looks like it's gonna take about thirty seconds to download. I won't make you watch that.

16:21

We're gonna come back over here, and we're gonna take this command, and we're gonna put you there. And you can pass a second, um, you can pass a second parameter here, and this is either, I think, out or error. I think there's two strings, out or error, and this is the actual output from down here.

16:43

Now, let us see if, um... Yeah, you're still working. That's fine. That's why, that's why we clean up temp files, 'cause I do stuff like that all the time. Now, if we run this, um, let's do this. This line downloading remaining.

17:04

What's the shape of this? It is title. So, okay. Let's see what happens. Wah, wah. Nothing. Downloading VITESS with the founder. I feel like this should have... Let's do this.

17:25

Result equals DD result. Ah, we need to wait on this. So we'll say result wait throw. Um, and so this is more correctly, this is called, um, yeah, this is called process. Process wait throw.

17:46

So now we are actually waiting 'cause it starts in the background. Let's clear this out, run that, and hey, there we go. That is the, um, that second argument, that closure that we passed in, that's what's doing that. So that's echoing this output from the underlying process. Okay. So that we don't have to download this a bunch of times, I'm gonna take that random, uh, that random ID right there.

18:14

And it looks like it's extracting the audio, cool. It's extracting the audio to the correct destination. Uh, so let's just copy this guy, and then we are going to hard code this while we're testing. We're gonna say that, uh, we can undo that.

18:35

File name equals that. So now we can keep, now we can keep testing without, uh, without having to download it every single time. So the file name equals that, the path equals that. Now what do we need to do? We need to create, uh, we need to authorize an upload on Transistor. So we need to go to Transistor and say, "Hey, I'm about to upload a file.

18:56

I don't have anywhere to put it. Can you give me a bucket to put it in?" And they'll say, "Yes, sounds good." And then we will finally go and create the episode resource on Transistor, and that's it, then we're done. That's the last thing we need to do. Here are the docs for authorizing an upload, and it does say you, you probably already have a public bucket, and so you don't need to do this.

19:19

In this application, I don't have an S3 bucket attached, but if you did, you could simply move that file into the S3 bucket and then create a temporary signed URL or just make the file public, I don't really care, and then pass it into the episode audio URL. But because we don't have that, we're gonna say, "Hey, Transistor, help me out here." So we're gonna go to episodes, authorize upload URL, and we need to pass the file name.

19:42

Okay. Let's do that. We still have our client around, I think. Didn't we name that client Client? Yeah, we still have our client around. So we're going to get episodes, authorize upload... Boy, my memory is shot. Episodes, authorize, upload. And we're gonna set the file name to our file name.

20:06

Hmm, funny how that works. And we will throw and JSON. What does the response look like? It is wrapped in data, and then, actually, I just want the attributes. I want the upload URL and the audio URL. So what this is telling me is Transistor is using S3 and creating a pre-signed URL just for me, and then they're saying, "Hey, when you're done uploading that, this is the URL that you're gonna pass to create the episode." Pretty

20:36

good design, frankly. So we'll take, uh, data attributes. We'll take data attributes. Cool. And this is our authorization. You know what? We're not gonna test this. We're just gonna YOLO it. So the authorization is that. Then we want to actually upload to that, so we'll say HTTP attach.

20:59

Hey. Um, and attach takes a name and a resource. Okay, we can do that. We can totally do that. File name and F open path for reading. That seems fine to me.

21:22

So we're attaching a file, and that file is the one that we stored at the temp path. Attach file, and then what do we do with it? Um, audio MPEG. I think we just post, we put to the upload URL. Cool. And we're gonna put to the authorization, and what was the shape of that one?

21:46

Upload URL. Boy, if this works first try... That's not gonna work firth- first try . But if this works first try, that's gonna be very cool. And we'll just throw. I don't think we need to listen or do anything with that response because, um, we already have the important part right here, the audio URL.

22:09

I think that is all we need. Okay. So we've got, um... We need to move this into the right location. We're all over the place. Hey, it's gonna work. It's gonna work. It's gonna be fine. Now we have the audio URL, and let's create an episode. Um, so the episode, hmm, let's say new, that's a bad name.

22:34

Um, episode equals client, probably post, probably to episodes, and then a bunch of stuff. Let's go to episodes, and we want to, let's just search create an episode. All righty.

22:55

We need show ID and audio URL. Okay, that's fine. Show ID is our show ID, and our audio URL is in fact the authorization, not the upload URL, but the audio URL. Okay. Then, um, description.

23:18

I'm not gonna do all of this because that's kind of annoying to watch, but we do have some of it. Remaining description, and we'll do title as well. So we'll take you, and we'll take you and change that and just confirm is that right? Show ID, title.

23:40

Now here, here's the most important part. This is the means by which we connect everything, this video URL. So we must include the video URL because remember, that's how we, that's how we de-duplicate, that's how we check. And so we're just gonna come up here, and we'll just copy this part. We're basically just gonna reconstruct, uh, the video URL.

24:06

Okay. I think this might be right. Um, I don't wanna actually ... We, we need to... I, I don't wanna actually, uh, publish anything- Wow, we're, create a new draft episode. Okay. I don't wanna... This is live. We're doing it live, so I don't wanna accidentally publish a new episode that shows up in people's feeds while I'm recording this video.

24:32

Uh, so it does create a new draft episode, so we should be okay. I think the only thing I've done potentially wrong here is this doesn't look JSON API-ish to me. So let's come back and, oh, obviously. It, it's all gotta be wrapped. Um, that's fine. We'll just say episode is that, and we'll take you, you, br-br-br-br, br, br. There we go. Cool.

24:53

Now that is done. Um, I think there's an increment number. That doesn't matter right now, but increment number. Yeah, I always want that. I just want, I want it to be nice and smooth, so we'll increment the number, and we'll see. Let's, uh, DD episode. Um, okay.

25:15

Now let's do code, just to see. Uh, response, okay. Well, maybe we can't do code. What, how do you get the... Status. That's what I'm looking for. Status and episode. We're about to make an episode. Um, if this works first try, I'm gonna be very impressed with myself, and you should be too impressed with me, but I don't think it's gonna work, so I think we're in the clear.

25:40

I don't think we need to be impressed. Okay. We're going to authorize an upload with that filename. We're gonna attach it. Is this the right signature? Name and contents is right. What is filename? You know what? Let's just do, we'll do it, we'll do it all. Okay. Filename, filename. We're gonna put it there.

26:00

We're gonna throw, um, this line, uploading. Okay. So we'll know if we make it past the authorization, upload it, and then once it's safely stored at audio URL, we can create a new episode with the show ID, the audio URL, the title, the description, and the video URL.

26:24

Here we go, my friends. Now, remember, um, we already have... Look, this is why we put it in a temp folder, 'cause YTDLP left some stuff behind. Frankly, that's probably my fault. Uh, and we can delete that later with our cleanup script. July 22nd and July 7th. Okay. That's interesting. Um, all right.

26:44

So we've got, that's the one that we're after right there, IY1, and I think we just hard-coded that so we don't have to download it over and over and over and probably get banned by YouTube. Let's run it. Let's see what happens. AR sync. Not gonna work. It's not gonna work. Did it work? Did it actually work?

27:11

Status 201, baby. Okay. Let's go. Let's look. Transistor. Uh, not, no, I need episodes. You have one unpublished episode. Well, you have to be impressed.

27:32

I, I didn't think it was gonna work, but it, look, it worked right there. Welcome back to Data- Welcome back to Database School. Hey. That is pretty great, actually. That is, that's pretty great. Now, I can run this as a GitHub action. I could run this every, you know, hour as a GitHub action. What I actually do is I have this integrated into a broader application that manages much of my life, Life OS, if you will, and it runs every hour, and it ha- just

28:02

handles this for me. I think the last thing I wanna do is, is show you that, um, our, like, our matching system worked. We're gonna uncomment a bunch of stuff, and hopefully we will see, uh, the output that says, "No new episodes available." Let's run that now. Hopefully. Yep, yep, yep, yep. So we're actually gonna check, yeah, we're gonna check Transistor.

28:24

We're gonna check YouTube, and then we should see no new episodes. So if we run this again. Feels good, man. Feels good, man. Okay. That, my friends, that is how we automate our lives. We take, we take tasks that are, you know, five to 20 minutes that we have to do once a week, and we spend, I don't know, how long is this video?

28:51

We spend an hour writing a script, and now it's gonna work perfectly, and now we never have to think about it again. Thank you to Transistor for sponsoring this episode. I'm a huge fan of Transistor. I would highly encourage you to check them out for all of your podcast needs. Also, check out the Database School podcast. If you like databases, it's a great podcast. Until the next time, see ya.

Videos

More videos

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues