0

How to keep the file name and the extension the same and append _backup to the old file?

I have had tried this

find . -name "*.mp4" -exec bash -c 'for f; do ffmpeg -i "$f" -codec copy "${f%.*}.mp4"; done' -- {} +

but here the files would be overwritten.

I hope what I have requested is possible.

featertu
  • 33
  • 5
  • Does this answer your question? [How do you convert an entire directory with ffmpeg?](https://stackoverflow.com/questions/5784661/how-do-you-convert-an-entire-directory-with-ffmpeg) – Blackhole Apr 21 '21 at 14:04
  • No, because of the different file extensions there. – featertu Apr 21 '21 at 14:18

1 Answers1

1

I suggest you append "_backup" to your input files first, then process the just renamed files with ffmpeg:

Simple for-loop to process files in current directory:

for f in *.mp4; do
  mv "$f" "${f%.*}_backup.mp4"
  ffmpeg -i "${f%.*}_backup.mp4" -c copy "$f"
done

#or single-line:
for f in *.mp4; do mv "$f" "${f%.*}_backup.mp4"; ffmpeg -i "${f%.*}_backup.mp4" -c copy "$f"; done

find to process files in current directory and sub directories:

find -name "*.mp4" -exec bash -c '
  f="{}"
  mv "$f" "${f%.*}_backup.mp4"
  ffmpeg -i "${f%.*}_backup.mp4" -c copy "$f"
' \;

#or single-line:
find -name "*.mp4" -exec bash -c 'f="{}"; mv "$f" "${f%.*}_backup.mp4"; ffmpeg -i "${f%.*}_backup.mp4" -c copy "$f"' \;
Reino
  • 1,704
  • 9
  • 15
  • I have tried ```find . -name "*.mp4" -exec bash -c 'for f in *.mp4; do mv "$f" "${f%.*}_backup.mp4" ffmpeg -i "${f%.*}_backup.mp4" -c copy "$f" done' -- {} +``` But files in the nested folders are ignored. Do you have a fix? – featertu Apr 24 '21 at 17:03
  • @featertu You shouldn't randomly combine these commands. Please see my updated answer. – Reino Apr 25 '21 at 00:47
  • Thank you for you answer. Yet still, nested folders are being left out. Is it a problem with my setup? Does it work for you? – featertu Apr 25 '21 at 17:28
  • @featertu With `find -name "*.mp4"` alone I get a list of all mp4-files in the current dir as well it's sub-directories. I'm not an expert on possible differences between different versions of `find`. Sorry. – Reino Apr 25 '21 at 18:32