ym

来自Tsetien’s Wiki
跳转到导航 跳转到搜索

A very personal Windows Batch script for the downloading of music or audio tracks from YouTube to a local place (here in this case is D:\_WMD\) using a locally stored up-to-date Netscape-styled cookies.txt (here in this case, it is located at D:\Omnigena\cookies.txt) that automatically produced by yt-dlp. It requires the following software to be properly set up in %PATH% (here in my case is C:\_c\):

By default, this batch script will download the audiotrack from the given URL (yt-dlp), convert it to an intermediate .flac file (ffmpeg), re-encode it as .m4a (qaac), and then crop the thumbnail image downloaded from the URL to 1:1 (ffmpeg) and add it as the .m4a file’s cover image (Nero AAC Encoder). The script also accepts three switches set out as follows:

  • -a: changes to ya mode where the naming pattern is slightly different, the cover image is not cropped to 1:1, and the audio quality is slightly lower (-v 0-V 127);
  • -s: changes to yms mode where the exact frame at the 64th second of the video is extracted, proportionally scaled if needed by making its shorter edge at exactly 1,200 px, cropped to 1:1, and set as the cover image of the file;
    • (specially, if -ss n is used, then the given number n shall surpass the default value, 64, and be used for -ss in the final ffmpeg command;) and
  • -k: keeps the intermediate .flac file for future uses. Although it is a .flac file, it does not have and cannot produce any acoustic improvements; therefore it is only recommended to keep it if it shall be further edited to ensure there is no quality loss in re-encoding.

The batch

ym.bat
@echo off
chcp 65001 >nul
set "a="
set "c="
set "f="
set "k="
set "n="
set "s="
set "ss="
set "v=64"
set "u="
:a
if /i "%~1"=="" goto p
if /i "%~1"=="-a" (set "a=1") else if /i "%~1"=="-s" (set "s=1") else if /i "%~1"=="-k" (set "k=1") else if /i "%~1"=="-ss" (set "ss=1" & set "s=1") else (
	if /i "%ss%"=="1" (set "v=%~1" & set "ss=0") else (set "u=%~1")
)
shift
goto a
:p
if /i "%u%"=="" (goto :eof)
if /i "%a%"=="1" (
	set "c=%AppData%\yt-dlp\config-ya"
	set "f=%AppData%\yt-dlp\config-yan"
) else if /i "%s%"=="1" (
	set "c=%AppData%\yt-dlp\config-yms"
	set "f=%AppData%\yt-dlp\config-ymn"
) else (
	set "c=%AppData%\yt-dlp\config-ym"
	set "f=%AppData%\yt-dlp\config-ymn"
)
yt-dlp "%u%" --config-location "%c%"
for /f "delims=" %%a in ('yt-dlp "%u%" --config-location "%f%"') do set "n=%%a"
if /i "%a%"=="1" (
	ffmpeg -i "%n%.png" -y -compression_level 0 -vframes:v 1 -update true -vf "format=rgba64le,scale='if(gte(iw,ih),min(1680,iw),-2):if(lt(iw,ih),min(1680,ih),-2):flags=lanczos+accurate_rnd+full_chroma_inp+full_chroma_int:param0=4',format=rgba64le,setsar=1" "%n%_intermediateagzk3kXe8nDr.png"
) else if /i "%s%"=="1" (
	ffmpeg -i "%n%.mkv" -y -ss %v% -an -compression_level 0 -vframes:v 1 -update true -vf "format=rgba64le,crop='floor(min(iw,ih)/2)*2:floor(min(iw,ih)/2)*2',scale='if(lte(1200,iw),1200,iw):-2:flags=lanczos+accurate_rnd+full_chroma_inp+full_chroma_int:param0=4',format=rgba64le,setsar=1" "%n%_intermediateagzk3kXe8nDr.png"
) else (
	ffmpeg -i "%n%.png" -y -compression_level 0 -vframes:v 1 -update true -vf "format=rgba64le,crop='floor(min(iw,ih)/2)*2:floor(min(iw,ih)/2)*2',scale='if(lte(1200,iw),1200,iw):-2:flags=lanczos+accurate_rnd+full_chroma_inp+full_chroma_int:param0=4',format=rgba64le,setsar=1" "%n%_intermediateagzk3kXe8nDr.png"
)
cjpegli -q 95 --chroma_subsampling=444 "%n%_intermediateagzk3kXe8nDr.png" "%n%.jpg"
exiftool -overwrite_original -all= "%n%.jpg"
if /i "%a%"=="1" (qaac -N -V 127 "%n%.flac" -o "%n%.m4a") else (qaac -N -v 0 "%n%.flac" -o "%n%.m4a")
aact "%n%.m4a" -add-cover:front:"%n%.jpg"
if /i not "%n%"=="" (
	del /q "%n%.jpg"
	del /q "%n%*.png"
	del /q "%n%*.mp4"
	del /q "%n%*.mkv"
	del /q "%n%*.webm"
	if not defined k del /q "%n%.flac"
)

yt-dlp configuration files

These files shall be put into %AppData%\yt-dlp with no extension attached to their filenames.

NB
Please note that the second space in line 2 of the following config file—the one preceding %(title)s—is turned into a non-breaking space, which is not intended; this is due to an internal error by SyntaxHighlight or MediaWiki, which is not controllable by this site. Therefore, it is advised to manually replace it with a regular space before using this script in practice.
config-yan
--print filename
-o "D:\_WMD\[%(timestamp>%y%m%d,upload_date>%y%m%d,release_date>%y%m%d,modified_date>%y%m%d)s] %(title)s (%(id)s)"
--replace-in-metadata "title" "\: " "—"
--replace-in-metadata "title" "\?" ""
--replace-in-metadata "title" "\:" "-"
--replace-in-metadata "title" "[\\\/]" " · "
--replace-in-metadata "title" "[\"]" "'"
--replace-in-metadata "title" "[\|]" " · "
--replace-in-metadata "title" "  " " "
config-ymn
--print filename
-o "D:\_WMD\%(title)s (%(id)s)"
--replace-in-metadata "title" "\: " "—"
--replace-in-metadata "title" "\?" ""
--replace-in-metadata "title" "\:" "-"
--replace-in-metadata "title" "[\\\/]" " · "
--replace-in-metadata "title" "[\"]" "'"
--replace-in-metadata "title" "[\|]" " · "
--replace-in-metadata "title" "  " " "
NB
Please note that the second space in line 26 of the following config file—the one preceding %(title)s—is turned into a non-breaking space, which is not intended; this is due to an internal error by SyntaxHighlight or MediaWiki, which is not controllable by this site. Therefore, it is advised to manually replace it with a regular space before using this script in practice.
config-ya
--no-write-subs
--no-write-auto-subs
-f "bestaudio/best"
--extract-audio
--replace-in-metadata "title" "\: " "—"
--replace-in-metadata "title" "\?" ""
--replace-in-metadata "title" "\:" "-"
--replace-in-metadata "title" "[\\\/]" " · "
--replace-in-metadata "title" "[\"]" "'"
--replace-in-metadata "title" "[\|]" " · "
--replace-in-metadata "title" "  " " "
--parse-metadata ":(?P<meta_synopsis>)"
--parse-metadata ":(?P<description>)"
--parse-metadata ":(?P<purl>)"
--parse-metadata ":(?P<meta_description>)"
--parse-metadata ":(?P<meta_purl>)"
--parse-metadata "Web Downloads:%(album)s"
--parse-metadata "Various Artists:%(album_artist)s"
--embed-metadata
--embed-chapters
--parse-metadata "%(timestamp>%Y,upload_date>%Y,release_date>%Y,modified_date>%Y)s:%(meta_date)s"
--audio-format flac
--write-thumbnail
--convert-thumbnails png
--cookies "D:\Omnigena\cookies.txt"
-o "D:\_WMD\[%(timestamp>%y%m%d,upload_date>%y%m%d,release_date>%y%m%d,modified_date>%y%m%d)s] %(title)s (%(id)s).%(ext)s"
config-ym
--no-write-subs
--no-write-auto-subs
-f "bestaudio/best"
--extract-audio
--replace-in-metadata "title" "\: " "—"
--replace-in-metadata "title" "\?" ""
--replace-in-metadata "title" "\:" "-"
--replace-in-metadata "title" "[\\\/]" " · "
--replace-in-metadata "title" "[\"]" "'"
--replace-in-metadata "title" "[\|]" " · "
--replace-in-metadata "title" "  " " "
--parse-metadata ":(?P<meta_synopsis>)"
--parse-metadata ":(?P<description>)"
--parse-metadata ":(?P<purl>)"
--parse-metadata ":(?P<meta_description>)"
--parse-metadata ":(?P<meta_purl>)"
--parse-metadata "Web Downloads:%(album)s"
--parse-metadata "Various Artists:%(album_artist)s"
--embed-metadata
--embed-chapters
--parse-metadata "%(timestamp>%Y,upload_date>%Y,release_date>%Y,modified_date>%Y)s:%(meta_date)s"
--audio-format flac
--write-thumbnail
--convert-thumbnails png
--cookies "D:\Omnigena\cookies.txt"
-o "D:\_WMD\%(title)s (%(id)s).%(ext)s"
config-yms
--no-write-subs
--no-write-auto-subs
-f "bv*[height<=?4200][width<=?4200]+ba/b"
-S "res:2900"
-k
--extract-audio
--replace-in-metadata "title" "\: " "—"
--replace-in-metadata "title" "\?" ""
--replace-in-metadata "title" "\:" "-"
--replace-in-metadata "title" "[\\\/]" " · "
--replace-in-metadata "title" "[\"]" "'"
--replace-in-metadata "title" "[\|]" " · "
--replace-in-metadata "title" "  " " "
--parse-metadata ":(?P<meta_synopsis>)"
--parse-metadata ":(?P<description>)"
--parse-metadata ":(?P<purl>)"
--parse-metadata ":(?P<meta_description>)"
--parse-metadata ":(?P<meta_purl>)"
--parse-metadata "Web Downloads:%(album)s"
--parse-metadata "Various Artists:%(album_artist)s"
--embed-metadata
--embed-chapters
--parse-metadata "%(timestamp>%Y,upload_date>%Y,release_date>%Y,modified_date>%Y)s:%(meta_date)s"
--audio-format flac
--cookies "D:\Omnigena\cookies.txt"
-o "D:\_WMD\%(title)s (%(id)s).%(ext)s"
--merge-output-format mkv