KISS 🇺🇦

Stop the war!

Stop the war in Ukraine! Fuck putin!

More information is at: https://war.ukraine.ua/.

There is a fund to support the Ukrainian Army: https://savelife.in.ua/en/donate/, and there is a special bank account that accepts funds in multiple currencies: https://bank.gov.ua/en/about/support-the-armed-forces. I donated to them. Please donate if you can!

Killer putin

Killer putin. Source: politico.eu.

Arrested putin

"It hasn't happened yet, but it will happen sooner or later. Beautiful photo, isn't it?" Source: twitter.

Rename downloaded youtube videos with mod time

| comments

It started with me wanting to download youtube videos as music podcasts (because a number of interesting people post youtube videos even though they mostly don’t show anything; this is very annoying). This is easy to do on a computer (as opposed to a mobile device) with the amazing yt-dlp! One channel has multiple videos per day and I’d like to listen to them in order, so I need to include the publication timestamp at the beginning of the downloaded filenames. The issue is that the youtube API provides upload_date and release_date metadata as a date only, not including time; release_timestamp includes time, but isn’t always available.

It’s interesting that yt-dlp sets the modification time of the file to the last modification time of the remote file:

1
2
--mtime                         Use the Last-modified header to set the file
                                modification time (default)

So I decided to use it instead (yt-dlp can provide the modified_timestamp field, but I’ve never seen it for youtube videos, therefore there is no guaranteed way to extract the Last-Modified value for the --output parameter). yt-dlp conveniently has an option to rename files after downloading! A small script is necessary to get the file modtime, format it in a human-readable way and add it to the filename.

Using file modtime

stat can get the file modification time and format it in a reasonable way, here month-day and hour-minute:

1
2
$ stat -f %Sm -t %m%d_%H%M 1.mp3
0608_1739

To rename the downloaded files automatically, yt-dlp has the --exec option, and the suitable command is:

1
$ yt-dlp … --exec 'mv "%(filepath)s" "`dirname "%(filepath)s"`/`stat -f %Sm -t %m%d_%H%M "%(filepath)s"`_`basename "%(filepath)s"`"' https://www.youtube.com/channel/xxx

My full download-“sync” command is:

1
$ noti yt-dlp --ignore-errors --playlist-end 12 --download-archive archive.lst --output '%(title)s_(%(duration)ss)_%(id)s.%(ext)s' --format bestaudio --extract-audio --audio-format mp3 --audio-quality 2 --playlist-reverse --exec 'mv "%(filepath)s" "`dirname "%(filepath)s"`/`stat -f %Sm -t %m%d_%H%M "%(filepath)s"`_`basename "%(filepath)s"`"' --match-filter 'title *= "Podcast"' ytuser:xxx

The --match-filter provides a filter for episodes based on the title.

As I later found out, the modification timestamp tends to change over time even though the video seemingly stays the same. I suppose that happens when some metadata is updated. Yet this is good enough if I download new videos often.

Post-scriptum

ps. If you want to apply this change after the fact to the previously downloaded files, you can:

1
$ for f in *.mp3; do mv "$f" "`stat -f %Sm -t %m%d_%H%M "$f"`_$f"; done

Makefile

pps. At the end, I came up with a Makefile that can archive, clean and download episodes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
all: archive clean dl

.PHONY:
dl:
  noti yt-dlp --ignore-errors --playlist-end 12 --download-archive archive.lst --output '%(title)s_(%(duration)ss)_%(id)s.%(ext)s' --format bestaudio --extract-audio --audio-format mp3 --audio-quality 2 --playlist-reverse --exec 'mv "%(filepath)s" "`dirname "%(filepath)s"`/`stat -f %Sm -t %m%d_%H%M "%(filepath)s"`_`basename "%(filepath)s"`"' ytuser:xxx

URL = ""
.PHONY:
dl-one:
  noti yt-dlp --ignore-errors --download-archive archive.lst --output '%(title)s_(%(duration)ss)_%(id)s.%(ext)s' --format bestaudio --extract-audio --audio-format mp3 --audio-quality 2 --exec 'mv "%(filepath)s" "`dirname "%(filepath)s"`/`stat -f %Sm -t %m%d_%H%M "%(filepath)s"`_`basename "%(filepath)s"`"' $(URL)

.PHONY:
clean:
  @ls *mp3 | fzf -m --print0 --header='Select files to *trash*' --tac | xargs -0 -t trash -F

.PHONY:
archive:
  @ls *mp3 | fzf -m --print0 --header='Select files to *archive*' --tac | xargs -0 -t -I{} mv {} backup

I can also download one episode by its URL: m dl-one URL="https://youtube.com/watch?v=xxx".

Comments