{"id":6284,"date":"2021-10-12T11:04:36","date_gmt":"2021-10-12T15:04:36","guid":{"rendered":"https:\/\/peroty.com\/blog\/?p=6284"},"modified":"2021-10-12T11:04:36","modified_gmt":"2021-10-12T15:04:36","slug":"downloading-audio-from-youtube-using-youtube-dl","status":"publish","type":"post","link":"https:\/\/peroty.com\/blog\/tinkered-with\/downloading-audio-from-youtube-using-youtube-dl\/","title":{"rendered":"Downloading audio from Youtube using youtube-dl"},"content":{"rendered":"\n<p>I saw Mike over at Initial Charge link to <a href=\"https:\/\/initialcharge.net\/2021\/10\/listenbox-service\/\">Listenbox Turns YouTube Channels Into Podcasts<\/a>. This is a neat idea and something I&#8217;ve been doing sparingly with longer-form Youtube content that is mainly audio.<\/p>\n\n\n\n<p>However, I don&#8217;t need to pay a subscription for another thing in my life I&#8217;ll use on occasion. So I tweaked my Youtube-dl setup to pull audio from Youtube videos instead of the video itself.<\/p>\n\n\n\n<p>First, I have to thank Jason for his <a href=\"https:\/\/burk.io\/2020\/my-youtube-dl-setup\">youtube-dl setup<\/a> which I replicated in a container on my server at home. He&#8217;s updated his configuration to support M1 Macs and some fancy iPhone shortcuts. <\/p>\n\n\n\n<p>I have mine running on Ubuntu sitting in a container in proxmox in my office. <\/p>\n\n\n\n<p>I&#8217;ve created a small shell script called <code>music.sh<\/code>. The contents are below, which I&#8217;ll explain.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># YTDL-Music Playlist - Audio Only - Goes to Music folder\n\/usr\/local\/bin\/youtube-dl \\\n--extract-audio  --audio-format mp3  --embed-thumbnail --ignore-errors \\\n-o \"\/mnt\/Youtube\/Music\/%(title)s.%(ext)s\" \\\n--download-archive \/var\/www\/video\/music.txt \\\nhttps:&#47;&#47;www.youtube.com\/playlist?list=PL9C818BCBDA822A24<\/code><\/pre>\n\n\n\n<p>The first line is a comment reminding me of what this is and does.<\/p>\n\n\n\n<p><code>\/usr\/local\/bin\/youtube-dl<\/code> &#8211; The location where I have youtube-dl installed.<\/p>\n\n\n\n<p><code>--extract-audio<\/code> &#8211; Downloads only the audio and discards the video.<\/p>\n\n\n\n<p><code>--audio-format mp3<\/code> &#8211; Sets the format to MP3 for compatability.<\/p>\n\n\n\n<p><code>--embed-thumbnail<\/code> &#8211; Embeds the thumbnail from the Youtube video. Sometimes useful, sometime not. But it&#8217;s nice to have some kind of artwork on the file.<\/p>\n\n\n\n<p><code>--ignore-errors<\/code>` &#8211; Skips errors like if a video is removed or not able to be downloaded for some reason.<\/p>\n\n\n\n<p><code>-o \"\/mnt\/Youtube\/Music\/%(title)s.%(ext)s\"<\/code> &#8211; The <code>-o<\/code> stands for output and tells youtube-dl where to save the file. In my case, I have it saved to my <abbr title=\"network-attached storage\">NAS mounted through NFS at <code>\/mnt\/Youtube\/Music.<\/code><\/abbr><\/p>\n\n\n\n<p>The title and extension are pulled right from <a href=\"https:\/\/github.com\/ytdl-org\/youtube-dl#output-template-examples\">an example output<\/a>. I don&#8217;t know what the lowercase <code>s<\/code> means. I just used it because the examples showed it.<\/p>\n\n\n\n<p><code>--download-archive \/var\/www\/video\/music.txt<\/code> &#8211; I write a log of everything I download so if I delete it after watching it, it doesn&#8217;t get pulled back down every single time. The file logs the youtube ID as opposed to the title or something human-readable but it&#8217;s better than nothing.<\/p>\n\n\n\n<p>Finally, the URL links to the Youtube playlist where I want to download from. This makes triggering the downloads as simple as adding a file to that playlist and letting youtube-dl do the rest.<\/p>\n\n\n\n<p>How does youtube-dl know to check the list for new videos? I have a cron job set to run the script every 10 minutes. So anything newly added to the playlist will get downloaded.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*\/10 * * * * sh \/var\/www\/video\/music.sh<\/code><\/pre>\n\n\n\n<p>Once I download the audio, it gets picked up by Plex. Plex is set to watch that folder and add new content it finds to the library as audio. So I can download music, podcasts uploaded to Youtube, or technical shows where the content is largely audio. <\/p>\n\n\n\n<p>It works well other than the hiccups any self-hosted setup can run into and it costs my nothing additional since I&#8217;m already paid for the computer and pay for the electricity\/internet anyway at home.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I saw Mike over at Initial Charge link to Listenbox Turns YouTube Channels Into Podcasts. This is a neat idea and something I&#8217;ve been doing sparingly with longer-form Youtube content that is mainly audio. However, I don&#8217;t need to pay a subscription for another thing in my life I&#8217;ll use on occasion. So I tweaked [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1415],"tags":[1463,451,1412,832,1462],"class_list":["post-6284","post","type-post","status-publish","format-standard","hentry","category-tinkered-with","tag-audio-download","tag-podcast","tag-self-hosting","tag-youtube","tag-youtube-dl"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/peroty.com\/blog\/wp-json\/wp\/v2\/posts\/6284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peroty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peroty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peroty.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/peroty.com\/blog\/wp-json\/wp\/v2\/comments?post=6284"}],"version-history":[{"count":1,"href":"https:\/\/peroty.com\/blog\/wp-json\/wp\/v2\/posts\/6284\/revisions"}],"predecessor-version":[{"id":6285,"href":"https:\/\/peroty.com\/blog\/wp-json\/wp\/v2\/posts\/6284\/revisions\/6285"}],"wp:attachment":[{"href":"https:\/\/peroty.com\/blog\/wp-json\/wp\/v2\/media?parent=6284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peroty.com\/blog\/wp-json\/wp\/v2\/categories?post=6284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peroty.com\/blog\/wp-json\/wp\/v2\/tags?post=6284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}