-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I rename previously-downloaded files with nothing but the youtube video ID? #5692
Comments
I'm doing this right now to move videos into folders named after the channel, what I'm doing is this, where u is the folder/channel name and url is the channel url:
The key here is the -s and not passing in --download-archive |
Here's a solution using bash and yt-dlp (and no other utils).
Edit the commented lines as necessary: #!/usr/bin/env bash
outtmpl="%(upload_date)s-%(id)s-%(title)s.%(ext)s" # adjust to your desired output template
archive="/path/to/archive/downloaded.txt" # replace with actual archive file path
download_path="/path/to/downloads/folder" # replace with actual directory path
pattern='^youtube(:\w+)?\s([A-Za-z0-9_-]{10}[AEIMQUYcgkosw048])$'
downloads=("${download_path}/"*)
while read -r input; do
[[ "${input}" =~ ${pattern} ]] || continue
input_id="${BASH_REMATCH[2]}"
filename=$(yt-dlp --no-download-archive -o "${outtmpl}" --get-filename -- "${input_id}")
[[ "${filename}" =~ ${input_id} ]] || { echo "${input}" >> "retry_archive.txt"; continue; }
for download in "${downloads[@]}"; do
[[ "${download}" =~ ${input_id} && -f "${download}" ]] || continue
final_path="${download_path}/${filename%.*}.${download/#*.}"
[[ "${download}" == "${final_path}" ]] && continue
mv -- "${download}" "${final_path}" || break 2
done
done < "${archive}" |
Any idea how to approach this on Windows? |
Python version using
|
My Linux abilities are very limited. Can i rather put this batch file in a folder where the files that need to be renamed are, edit it, and run it so it will rename all the files in this specific folder? But i also need to know what to edit to make it work properly.. |
The top three lines :: Desired output template
set "outtmpl=%%(upload_date)s-%%(id)s-%%(title)s.%%(ext)s"
:: Path to your download archive
set "archivePath=P:\ath\to\archive.txt"
:: Path to the folder containing downloaded files
set "downloadPath=P:\ath\to\download\folder\" Please be careful when using Batch as it might break easily. prefer the Python or Bash versions if possible. |
Thanks! Can i eliminate archive since i only have a folder with the video files and haven't used archive? :: Path to your download archive
set "archivePath=P:\ath\to\archive.txt" |
No. None of these scripts will work without a download archive file. Furthermore, any videos without entries in your archive would not be renamed. |
Even if i don't want to use the ID in the file name? Let's just say: |
All of the above scripts rely on the ID values from the archive to find the files that are to be renamed. There's no point in trying to use them without an archive. The bash solution states that it will only work if you intend to have the ID in the final filename. IMO it's not a good idea to strip the ID from the filename anyways, as that would make it very difficult for you if you wanted to rename them again using metadata values. For a solution that would work without a download archive, all of the youtube videos you want renamed would need to have been originally named with the same output template, or at least very similar output templates (e.g. the youtube ID is always at the end of the filename), and the script would need to be written to handle that specific naming scheme. |
bashonly's scriptHere's a solution using bash and yt-dlp (and no other utils).
Edit the commented lines as necessary: #!/usr/bin/env bash
outtmpl="%(upload_date)s-%(id)s-%(title)s.%(ext)s" # adjust to your desired output template
archive="/path/to/archive/downloaded.txt" # replace with actual archive file path
download_path="/path/to/downloads/folder" # replace with actual directory path
pattern='^youtube(:\w+)?\s([A-Za-z0-9_-]{10}[AEIMQUYcgkosw048])$'
downloads=("${download_path}/"*)
while read -r input; do
[[ "${input}" =~ ${pattern} ]] || continue
input_id="${BASH_REMATCH[2]}"
filename=$(yt-dlp -o "${outtmpl}" --get-filename -- "${input_id}")
[[ "${filename}" =~ ${input_id} ]] || { echo "${input}" >> "retry_archive.txt"; continue; }
for download in "${downloads[@]}"; do
[[ "${download}" =~ ${input_id} && -f "${download}" ]] || continue
final_path="${download_path}/${filename%.*}.${download/#*.}"
[[ "${download}" == "${final_path}" ]] && continue
mv -- "${download}" "${final_path}" || break 2
done
done < "${archive}" This is exactly what I had in mind. Should I close the issue since my problem is solved or leave it open for the benefit of windows/mac users? |
It's worth mentioning that the bash script silently failed to change the filenames when I had these options in my
I'm pretty sure only the output line caused trouble since |
The |
While the question was technically out of scope, this is a frequently asked question - so I have pinned it. If anyone has better/alternate scripts for automating this, feel free to continue posting them here |
I wrote a bash script to rename Twitch VODs previously downloaded by yt-dlp and use a new output template. It doesn't require an archive.txt, instead it gets all its information from the existing files. It's cluttered with features specific to my use-case and it assumes a plex-friendly media library layout but it might be a helpful starting place for someone. It also does timezone conversion from Unix epoch timestamp to localized ISO-8601, which I needed for something else. https://gist.github.com/rileywbaker/d58dec09e43027d26b95d2a219953a54 |
DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE
Checklist
Please make sure the question is worded well enough to be understood
I have already checked #3301 but it references a JSON file I don't have.
I do have a
downloaded.txt
in addition to each file having the ID in its filename.How can I use
yt-dlp
and/or other tools to rename every previously-downloaded video? In particular I'm interested in adding the upload date to every filename.Provide verbose output that clearly demonstrates the problem
yt-dlp -vU <your command line>
)[debug] Command-line config
) and insert it belowComplete Verbose Output
No response
The text was updated successfully, but these errors were encountered: