3

I found a bash script for my Synology NAS that is looking for *.mp4 files in a folder, strips the video out and saves them as *.m4a. (The purpose is an automatic 'YouTube to podcast converter').

for f in *.mp4; do mv -- "$f" "$(date +%Y-%m-%d -r "${f}") $f"; done
for f in *.mp4; do ffmpeg -i "$f" -vn -acodec copy "${f%.mp4}.m4a"; done

Sometimes the *.mp4 file names contain periods, e.g. 'This video...mp4', resulting in the podcast player not recognizing such files.

Is there a line I can add to the script to remove period(s) in front of the extension or to just remove them all?

user2359595
  • 33
  • 1
  • 4

4 Answers4

3

Use the rename command to replace multiple consecutive dots with just one:

rename 's/\.+/./g' *.mp4

If you have perl installed and you cannot find rename, try also prename.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
  • Thanks for the pointer and the command line. Unfortunately my build of linux doesn't seem to support the rename command (not found). Cheers – user2359595 May 07 '13 at 19:56
  • Try `prerename`, of which `rename` is an alias. It is installed with `perl` package in Ubuntu and Debian. – Stefano Sanfilippo May 07 '13 at 20:00
  • 1
    This is a good solution if you've got the Perl-based `rename` command. Unfortunately, there are some under-powered variants called `rename` that don't use Perl regular expressions which are ... well, less satisfactory than the Perl-based commands of the same name. See [How to rename with prefix/suffix](http://stackoverflow.com/questions/208181/how-to-rename-with-prefix-suffix/208389#208389) for a usable Perl `rename`. – Jonathan Leffler May 07 '13 at 20:05
  • What does `perl -v` says? If you cannot find it, I've pasted a copy here: http://codepad.org/IWaCTnbY – Stefano Sanfilippo May 07 '13 at 20:05
  • Thanks Stefano. It also wouldn't find 'prerename'. perl -v says 'This is perl, v5.8.6 built for MARVELL_88F6281' I'm not sure how to handle perl scripts, never did this before. – user2359595 May 07 '13 at 20:15
  • Nice! Download the script linked above, drop it in a folder, make it executable with `chmod +x rename.pl` and try calling it. – Stefano Sanfilippo May 07 '13 at 20:26
  • Noticed now: `prename`, not `prerename`! If you have `prename` you are ok. – Stefano Sanfilippo May 07 '13 at 20:29
  • 1
    @StefanoSanfilippo, the `${f}` is not outside any quotes: the `$()` runs in a subshell, so you effectively get a new "scope" for quotes. – glenn jackman May 07 '13 at 20:30
  • I had to install rename using `sudo apt install rename` – alper Sep 29 '21 at 13:56
2

Remove the extension from ${f}, then delete or substitute all dots, then add the extension back when you rename the files essentially dealing with all but the last dot:

#!/bin/bash

for f in *.mp4; do
        f=${f%.mp4}
        mv -- "${f}.mp4" "$(date +%Y-%m-%d -r "${f}.mp4") ${f//./_}.mp4"
done
for f in *.mp4; do
        ffmpeg -i "${f}" -vn -acodec copy "${f%.mp4}.m4a"
done

Obviously this only works for a single extension (.mp4 in this case).

Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
1

Try the following, which uses tr to remove any periods in the filename:

for f in *.mp4; do mv -- "$f" "$(date +%Y-%m-%d -r "${f}") $f"; done
for f in *.mp4; do ffmpeg -i "$f" -vn -acodec copy "`echo "$f" | tr -d "\."`.m4a"; done
Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
0

Just came across this answer here.
You can use the rename package by installing it using the following command.

sudo apt install rename

Then go to the directory containing the files and use the following command:

rename 's/\.(?!mp4$)/ /g' *.mp4
Somnath Rakshit
  • 555
  • 1
  • 6
  • 17