--- title: one liner to get youtube info subtitle: Never Gonna Give You Up date: 2023-01-21 keywords: [youtube, youtubedl, jq, shell, bash] --- It's a work in progress, but this will grab info about a youtube video and spit out some json. Requires [youtube-dl], [jq] and [jq-duration] (for changing duration from seconds to something more human readable). Swap the ID `$i` for either the ID or full URL of the video as needed. ``` bash youtube-dl -j $i | jq 'include "duration"; { "title": .title, "duration": (.duration|duration(2)), "url": .webpage_url, "channel": .channel, "channel_url": .channel_url }' ``` The above produces something like this: ``` json { "title": "Rick Astley - Never Gonna Give You Up (Official Music Video)", "duration": "3m 32s", "url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ", "channel": "Rick Astley", "channel_url": "https://www.youtube.com/channel/UCuAXFkgsw1L7xaCfnd5JJOw" } ``` I made a [jinja] template to make markdown text to use with the above : ``` markdown * [{{ title }}]({{ url }}) ({{ duration }}) from [{{ channel }}]({{ channel_url }}) ``` To use this, put the above in a file called (eg `yt2md.j2`) and call it by appending `| jinja2 yt2md.j2 -` to the `youtube-dl` command. (*note:* the `-` appended to the end is required to get around a jinja2 cli bug) The full command will then look like: ``` bash youtube-dl -j $i | jq 'include "duration"; { "title": .title, "duration": (.duration|duration(2)), "url": .webpage_url, "channel": .channel, "channel_url": .channel_url }' | jinja2 yt2md.j2 - ``` [youtube-dl]: https://youtube-dl.org [jq]: https://stedolan.github.io/jq/ [jq-duration]: https://github.com/fearphage/jq-duration [jinja]: https://jinja.palletsprojects.com/en/3.1.x/