Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 23885  / 1 Year ago, wed, december 7, 2022, 6:57:04

I use the command line utility youtube-dl to download videos from YouTube and make mp3s from them with avconv. I'm doing this under Ubuntu 14.04 and very happy with it.



The utility downloads the files and saves them with the following name scheme:



TITLE(artist-track)-ID.mp3


So an actual filename looks like:



EPIC RAP BATTLE of MANLINESS-_EzDRpkfaO4.mp3


Some other file names in the folder look like:



EPIC RAP BATTLE of MANLINESS-_EzDRpkfaO4.mp3
Martin Garrix - Animals (Official Video)-gCYcHz2k5x0.mp3
Stromae - Papaoutai-oiKj0Z_Xnjc.mp3


At first, this was no problem. It didn't bother me while listening to my music in Rhytmbox.
But when moving to phone or other devices it is pretty confusing to see a so long name, and some players, like the Samsung ones, treat that last part (id after second dash) of the name as Album or something.



I'd like to create a bash script that removes what's after the second dash in the name for all files, so it'll make them like this:



From: Martin Garrix - Animals (Official Video)-gCYcHz2k5x0.mp3



To: Martin Garrix - Animals (Official Video).mp3



Is it also possible to instruct youtube-dl to exclude the ID from now on?



I am currently downloading with the command:



youtube-dl --extract-audio --audio-quality 0 --audio-format mp3 URL

More From » bash

 Answers
2

sed can accomplish this in a single line, albeit in a rather convoluted way.



ls | sed 's/(.*)(-.*)/mv "&" "1.mp3"/' | bash


This first lists the files in the current directory (assuming all the files you want to rename are in the current directory), and then uses sed's s/regex/replacement command to generate a sensible mv command which is then piped to bash which executes it. This assumes all of your files are something of the form "A-C.mp3" or "A - B-C.mp3". Here is how it works:



The regex part of the sed command is



(.*)(-.*)


this "groups" the name into two groupings (delimited with escaped parentheses): one matching ".*" (any number of any character) and another matching "-.*", a dash followed by any number of any character. Notice that this matches the entire filename (in two groups). Also note that since "greedy" regex is used, the first group will match "A" in "A-C.mp3" and "A - B", not just "A ", in "A - B-C.mp3", as wanted.



The replacement part of the sed command is



mv "&" "1.mp3"/


Note that an & character instructs sed to insert the entire pattern that matched regex, in this case that is the entire filename, and the 1 instructs sed to insert the part of the filename that matched the first grouping ".*". These two are combined with an preceding mv and a trailing .mp3, with escaped quotation marks to produce a sensible rename command. For example, for "A - B-C.mp3", the full sed command will produce:



mv "A - B-C.mp3" "A - B.mp3"


And finally all of this is piped to bash, which happily executes the mv (rename) command.


[#24690] Wednesday, December 7, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nsuainner

Total Points: 141
Total Questions: 102
Total Answers: 109

Location: Maldives
Member since Sat, Jan 29, 2022
2 Years ago
nsuainner questions
Sat, Oct 22, 22, 22:47, 2 Years ago
Sun, Oct 2, 22, 12:53, 2 Years ago
Tue, May 31, 22, 05:35, 2 Years ago
;