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:
12
--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:
12
$ 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:
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: