LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-08-2019, 02:20 AM   #1
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 181

Rep: Reputation: Disabled
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.
 
Old 11-08-2019, 03:26 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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

Last edited by Firerat; 11-08-2019 at 03:31 AM. Reason: highlight . (dot)
 
2 members found this post helpful.
Old 11-08-2019, 06:19 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Nice post, good advice.
 
Old 11-08-2019, 06:50 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Firerat View Post
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.
 
Old 11-09-2019, 12:47 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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...
 
  


Reply

Tags
convert, delete, ffmpeg



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copied files from SOURCE to TARGET but now want to rsync SOURCE to existing TARGET teddymills1 Linux - Newbie 3 10-28-2019 07:07 AM
iptables: rule with RETURN target just after a rule with ACCEPT target Nerox Linux - Networking 6 09-04-2011 03:33 PM
Making a symlink follow its target when the target moves pwabrahams Linux - Software 3 03-08-2011 07:04 PM
LXer: OggConvert makes Ogg converts (and converts to Oggs) LXer Syndicated Linux News 0 12-22-2007 06:00 AM
LXer: UFRaw 0.12 could make new converts to open source RAW photo conversion LXer Syndicated Linux News 0 08-29-2007 12:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration