LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-17-2016, 07:55 AM   #1
Steve W
Member
 
Registered: Mar 2007
Distribution: Linux Mint 18.1
Posts: 511

Rep: Reputation: 44
ffmpeg: batch process all files in folder to single channel


Hello

I sometimes wish to convert mp3 files to single channel only. I would normally do this in the shell by typing:

Code:
ffmpeg -v 5 -y -i "my_stereo_file.mp3" -acodec libmp3lame -ac 1 -ab 128k "my_stereo_file 1chan.mp3"
... the "ac 1" switch of course making the mp3 single channel, and I add "1 chan" to the filename to create an output file.


If I have a number of these to do, it would be good to automate the process. I have scripts that, for example, convert all m4a files in a directory to mp3:

Code:
for x in *.m4a; do ffmpeg -ab 140k -i "$x" "`basename "$x" .m4a`.mp3"; done
However I cannot figure out how to automatically add the "1 chan" bit to the filename. The m4a conversion script does not need to change the core filename, since it changes the extension to mp3 anyway.

Could anyone suggest the script line I would need to convert all mp3 files in that folder to mp3 with single channel, plus adding "1 chan" to each output filename as it does so?

(I think it has something to do with the Basename function, but having fiddled with this, I keep getting an "unexpected EOF error" when running, which I suspect is due to mismatched quotes, but I cannot figure out which types of quotes go where.)

Thank you for any advice you can give.

Steve

Last edited by Steve W; 06-17-2016 at 08:24 AM.
 
Old 06-17-2016, 08:50 AM   #2
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,669

Rep: Reputation: 496Reputation: 496Reputation: 496Reputation: 496Reputation: 496
You can place applicable descriptive chars or texts somewhere in end of this line --> "`basename "$x" .m4a`.mp3" before or after that bold file suffix. Just try to play it. You will find interesting tricks in computing.

Hope that helps.

m.m.
 
1 members found this post helpful.
Old 06-17-2016, 10:08 AM   #3
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 3,762
Blog Entries: 4

Rep: Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222
Code:
for i in *.m4a; do
    ffmpeg -i $i -ac 1 -vn -c:a libmp3lame -b:a 140k "${i%.*}"_1chan.mp3
done


for i in *.mp3; do
    ffmpeg -i $i -ac 1 -vn -c:a libmp3lame -b:a 128k "${i%.*}"_1chan.mp3
done
You can test that first with:
Code:
for i in *.m4a; do
    echo "${i%.*}_1chan.m4a"
    sleep 1
done

Last edited by teckk; 06-17-2016 at 10:11 AM.
 
1 members found this post helpful.
Old 06-18-2016, 06:33 AM   #4
Steve W
Member
 
Registered: Mar 2007
Distribution: Linux Mint 18.1
Posts: 511

Original Poster
Rep: Reputation: 44
Thank you for your solution. I had to put double quotes around the $i, or it couldn't handle filenames of more than one word.

I also got an error saying "Unrecognized option 'c:a'", but I suspect this was due to my ffmpeg version not being the latest. It tells me that I should use "avconv" now, so I replaced "ffmpeg" with "avconv" in your line, and it then worked perfectly!

So my final function is:

Code:
for i in *.mp3; do
    avconv -i "$i" -ac 1 -vn -c:a libmp3lame -b:a 128k "${i%.*}"_1chan.mp3
done
Thank you once again. Didn't need "basename" after all!

I struggle to understand Regular Expressions. Could you please tell me what the percent sign does in the output part of the command ("${i%.*}")? I see that .* means wildcard, but having Googled this, I can find no reference to a percent sign doing anything in this situation. Although I realise symbol in Regular Expressions have multiple uses!

Steve
 
Old 06-18-2016, 10:34 AM   #5
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 3,762
Blog Entries: 4

Rep: Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222
A few examples:

i="my_file.txt"
Code:
#Get file basename 
echo ${i%.*}
Code:
#Get file extension 
echo ${i#*.}
Code:
#Remove _ in filename
echo ${i//_/}
Code:
#Get filename before _
echo ${i%_*}
Code:
#Get filename after _
echo ${i#*_}
i="my file.txt"
Code:
#Replace space with _
${i// /_}
Code:
#Remove space in filename
echo ${i// /}
 
1 members found this post helpful.
Old 06-19-2016, 01:14 PM   #6
Steve W
Member
 
Registered: Mar 2007
Distribution: Linux Mint 18.1
Posts: 511

Original Poster
Rep: Reputation: 44
Ah, so the percent sign in this context means "get the string before the next character". I see. I was mistaking ".*" for the wildcard, but it's the character indicator.

I just tried
Code:
echo ${i%l*}
and it produced "my_fi".

Thanks for the information! We live and learn a little each day...

Steve
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
searching a particular string in all files of a single folder dinakumar12 Linux - Newbie 5 06-27-2011 03:23 PM
shell script to batch process-modify ip numbers in files. SupermanInNY Programming 5 08-13-2009 04:03 AM
Copy files from various locations to a single destination folder deostroll Linux - Newbie 5 05-13-2009 07:52 AM
Batch convert video in entire folder using ffmpeg Steven Hyde Linux - General 4 05-03-2008 06:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:37 PM.

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