简体   繁体   中英

MP3 sound volume with a bash script

I succeeded to turn up the volume of the sound on mp3 files recursively. First in my music directory I did

ls > ls.txt

Then I use this script :

while IFS= read -r line; do
find /your/directory/path/"$LINE" -type f -iname '*.mp3' -exec lame --scale 2  {} \; -exec mv {}".mp3" {} \;
done < ls.txt

Lame add.mp3 to each transformed-file name so I replace the original by the new while keeping the same file name. Do you know a more elegant code? Thank you

Your code breaks if there is a filename with a newline character in it. This is pretty rare, and you may ignore it.

Other, more serious problems:

Further, you are storing each line in the file ls.txt into the variable line , but you never use this variable, but instead access a variable named LINE .

ls.txt contains all the entries in the working directory without path component, while your find command searches those entries in the directory /your/directory/path/ . Therefore you either omit the path component (assuming that you are in the "right" directory anyway), or that you generate the file list using absolute directories.

Actually, unless you need the file ls.txt again in a different place, I would not even bother creating it, but instead just write

if cd /your/directory/path
then
  find * -name '*.mp3' ....
else
  echo Error: Directory not accessible >&2
fi      

Another possibility would be to get rid of the find command altogether and do a

shopt -s globstar # Enable ** globbing
shopt -s nullglob # Skip loop if there is no file
if cd /your/directory/path
then
  for mp_entry in **/*mp3
  do
    lame ....
    mv ....
  done
else
  echo Error: Directory not accessible >&2
fi      

The disadvantage of this solution is that it could fail, if the directory tree contains a such large number of matching files, that the expanded glob pattern would overflow the command line limit for bash. This size is not defined by bash itself, but depends on the operating system. You can get this size by doing a getconf ARG_MAX and then decide whether or not this could be a problem for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM