Yusuf Adeyemo
Yusuf Adeyemo

Reputation: 399

How do handle filename space and Character with FIND command

Good day all, I am working on a bash script to merge multiple mp3 files to one. The code is working fine but cannot handle file name with space or Characters. Below is the code. Can you please tell me what I'm doing wrong. Thanks

for file in $(find . -type f -name "*.mp3" -print0 | xargs -0 ); do
  ffmpeg -i "concat:intro.mp3|"$file"|outro.mp3" -acodec copy "${file%.mp3}-".mp3;
done

Upvotes: 0

Views: 547

Answers (3)

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

read builtin with -d followed by empty argument to use NUL caracter as input record delimiter

while IFS= read -r -d '' file; do
    ffmpeg -i "concat:intro.mp3|$file|outro.mp3" -acodec copy "${file%.mp3}-".mp3;
done < <(find . -type f -name "*.mp3" -print0)

Upvotes: 2

oliv
oliv

Reputation: 13249

You can use find together with bash -c command (that allows passing 2 arguments to ffmpeg):

 find . -type f -name "*.mp3" -exec bash -c 'ffmpeg -i "concat:intro.mp3|$1|outro.mp3" -acodec copy "${1%.mp3}-.mp3"' _ {} \;

Upvotes: 1

uml&#228;ute
uml&#228;ute

Reputation: 31284

find has an -exec flag that allows you to call scripts with the search results.

e.g. creating a simple helper-script addxtros.sh:

#!/bin/sh
infile=$1
outfile=${infile%.mp3}-.mp3 
ffmpeg -i "concat:intro.mp3|${infile}|outro.mp3" -acodec copy "${outfile}"

you can use it like:

find . -type f -name "*.mp3" -exec ./addxtros.sh {} ";"

Upvotes: 2

Related Questions