Rename file and encapsulte last X number of charactors, before the extension, in square brackets

0

I've been using youtube-dl to download an entire youtuber's channel for archival purposes. I had been using the default naming pattern for naming the videos which uses the video's title, then a hyphen, the 11 character video id and finally, the file's extension.

e.g.

title of the video - (an example)-oHg5SJYRHA0.mp4

For future downloads, I've removed the hyphen and placed the video id within square brackets. The problem is that I've already got hundreds of videos following the old naming scheme that I don't want to waste bandwidth on re-downloading just to get the new naming scheme.

How can I rename files of varying length so that a "]" is placed before the ".mp4" and they hyphen, 11 spaces/characters before the "].mp4" is replaced with a "["?

e.g.

title of the video - (an example)[oHg5SJYRHA0].mp4

[EDIT] I forgot to specify that I'd prefer a solution that'll work on Linux and/or FreeNAS/BSD.

Hollis Hurlbut

Posted 2019-10-17T21:55:08.223

Reputation: 1

What system are you using? Do you have Perl, or Bash, or PowerShell available? – user1686 – 2019-10-18T05:02:51.243

I split my time between Linux, FreeNAS/BSD and Windows, so I'm not really limited much in regards to what OS or language I'll use but I'd prefer something FOSS or OS agnostic. – Hollis Hurlbut – 2019-10-18T18:13:27.757

Answers

1

Under Linux, you can use rename command like that:

rename 's/-\w{11}(?=\.mp4$)/[$&]/' *.mp4

Regex explanation:

-           # a hyphen
\w{11}      # 11 word characters
(?=         # positive lookahead, make sure we have after:
  \.mp4     # a dot followed by mp4
  $         # end of string
)           # end lookahead

Replacement:

[           # opening square bracket
$&          # the whole match (i.e. 11 characters)
]           # closing squarre bracket

If the 11 characters can be other than word character, use the following:

rename 's/-\K.{11}(?=\.mp4$)/[$&]/' *.mp4

Where . matches any character but newline.

Toto

Posted 2019-10-17T21:55:08.223

Reputation: 7 722

Thanks Toto. I forgot to say that I'd prefer something that'd work on Linux and even more so if it was a regex, as I'm kind of struggling to get my head around it at the moment. So I'll give your answer a whirl and see if it works as intended. I especially appreciate that you've explained how the regex works.

Thanks again. – Hollis Hurlbut – 2019-10-18T17:57:04.603

@HollisHurlbut: You're welcome, glad it helps.If it works for you, feel free to mark the answer as accepted, How to accept an answer

– Toto – 2019-10-18T18:44:03.580

I've tried out the regex and there are a couple of issue. First is that you made a typo, specifically you put ".txt" mid way through the command rather than ".mp4", rename 's/-\K.{11}(?=.txt$)/[$&]/' *.mp4. The second issue is that it doesn't remove the hyphen just before the last 11 characters. – Hollis Hurlbut – 2019-10-25T19:54:30.677

@HollisHurlbut: My bad, fixed ;) – Toto – 2019-10-26T09:28:50.017

-1

If you don't mind to use a 3rd party tool, which I would recommend for one time use, I would recommend advancedrenamer, it's portable and free.

Otherwise you can use powershell, this description tells you everything you need to know. Let us know if you get stuck.

Albin

Posted 2019-10-17T21:55:08.223

Reputation: 3 983

Thanks for the quick answer. Advanced Renamer looks to be an upgrade in many areas over my current app of choice (Bulk Rename Utility) and it got the job done quickly and easily. In that regard, I can't fault your answer but I could kick myself, becuase I really should have specified that I wanted a Linux / BSD friendly solution. That's the only reason I'm not selecting your reply as an answer.

Thanks again, Albin. – Hollis Hurlbut – 2019-10-18T17:51:50.937