LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Delete source after ffmpeg converts to target (https://www.linuxquestions.org/questions/linux-newbie-8/delete-source-after-ffmpeg-converts-to-target-4175663893/)

ericlindellnyc 11-08-2019 02:20 AM

Delete source after ffmpeg converts to target
 
Code:

find . -type f -name '*.wav' -exec bash -c 'ffmpeg -n -i "$0" -c:a libmp3lame -q:a 2 "${0/%wav/mp3}"' '{}' \;
I used the above code successfully to convert .wav files to .mp3 files using ffmpeg.

However, my drive fills up fast, and I tried incorporating a command to rm the wav file after convert, like this ..

Code:

find . -type f -name '*.wav' -exec bash -c 'ffmpeg -n -i "$0" -c:a libmp3lame -q:a 2 "${0/%wav/mp3}"' '{}' \; -exec bash -c 'rm "$0"' ‘{}’ \;
I tried many permutations of this, removing quotes around $0 (after rm), trying different kinds of quotes -- straight ones, slanting one way or the other -- even apostrophes in place of single quotes.

Results were, just hanging.
system doesn't recognize rm command.
or it doesn't recognize a file name with embedded spaces that's parsed into constituent words by the system.
In the latter two cases, ffmpeg goes to the next file, leaving target MP3s and source WAVs in the directory.

I know find can take multiple execs, & I think there's a way to modify the ffmpeg command to rm the source file.

I can't find anything on this. I think it's a matter of referencing the same variable in both -exec commands.

Firerat 11-08-2019 03:26 AM

you should only delete the source if ffmpeg completed without error

find is good at finding things
and sure, you can get it to execute things, but it is not really the best approach


this is a little bash script
Code:

#!/bin/bash
ErrorReport () {
  [[ $? != 0 ]] \
    && echo "Error converting $Filename" 1>&2
}
trap ErrorReport EXIT


allwavtomp3 () {
while read Filename
do
  ffmpeg -n -i "$Filename" \
    -c:a libmp3lame \
    -q:a 2 \
    "${Filename/%wav/mp3}" \
    || exit 1 && rm "$Filename"
done < <( find ${1:-.} -type f -name "*.wav" )
}

allwavtomp3 "$1"

it is a little dumb, ( it doesn't check the arg given is a directory )

Code:

wavtomp3.sh /path/to/dir/withwavs
if no arg, then it will search from the working directory

if the ffmpeg completes without error the .wav is deleted
if there was an error it exits printing filename to stderr

https://mywiki.wooledge.org/BashGuide
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html


or you could create a file ~/.bash_functions

Code:

wavtomp3 () {
Filename="$1"
ffmpeg -n -i "$Filename" \
  -c:a libmp3lame \
  -q:a 2 \
  "${Filename/%wav/mp3}" \
  || exit 1 && rm "$Filename"
done
}

allwavtomp3 () {
while read Filename
do
  ffmpeg -n -i "$Filename" \
    -c:a libmp3lame \
    -q:a 2 \
    "${Filename/%wav/mp3}" \
    || exit 1 && rm "$Filename"
done < <( find ${1:-.} -type f -name "*.wav" )
}


and
Code:

. ~/.bash_functions
# that is . ~/.bas...

also add that to your ~/.bashrc
/!\ note the . at the start


then
Code:

wavtomp3 /path/to/filename.wav
allwavtomnp3 /path/to/dir


syg00 11-08-2019 06:19 AM

Nice post, good advice.

rtmistler 11-08-2019 06:50 AM

Quote:

Originally Posted by Firerat (Post 6055436)
you should only delete the source if ffmpeg completed without error

This is the most important advice.

Along with my added:
  • Every time I've used a wildcard or loop to do something destructive, it does exactly that, destructs data.
  • In spite of my far less than 100% success rate with writing scripts or a complex command line perfectly the first time, the destruction part has pretty much worked perfectly, that very first time.
  • I've regretted this some number of times == lost count
  • So I don't do this and I follow the fundamental first line advice which Firerat has offered.

Keep enough space.

Keep the newly converted stuff in a new directory.

Verify it first, and then delete your originals.

ondoho 11-09-2019 12:47 AM

ericlindellnyc, are you now using some kind of Linux?
One of your last threads, took us like 20 posts to find out you run iOS...


All times are GMT -5. The time now is 07:17 PM.