LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-13-2010, 07:52 PM   #1
jdavis2
Member
 
Registered: Feb 2007
Distribution: Fedora, Mandrake, Knoppix, Windows XP
Posts: 37

Rep: Reputation: 15
what regex expression replaces file extension using bash script


What I was trying to ask is will anyone help me to complete a bash script that will take filename from the command line then do the conversion that I have asked it to do in the script and also assign a new extension of my choice that I will include in the script as in the following:

convert_this file.mp4

then the script will process and return a result with a new extension, sort of like this:

file.avi

What I am doing now is entering the comand like this:

convert_this file.mp4 file.avi using the following script.

ffmpeg -i $1 -r 20 -vcodec mpeg4 -vtag DX50 -b 512k -acodec mp3 -ab 128k -bufsize 4m -qmax 51 -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -ar 44100 -g 300 -s 224x176 -aspect 4:3 -ac 2 -y $2

Last edited by jdavis2; 01-13-2010 at 08:04 PM. Reason: I didn't mean to asked that exact question. What i really meant to do was to use that phrase to search for similar posts.
 
Old 01-13-2010, 09:30 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Code:
FILE=$1

FULLNAME=$(basename $FILE)
EXT=$(echo $FULLNAME | sed 's/^.*\.//')
NAME=$(basename $FILE .${EXT})
NEWEXT=mpg
NEWNAME=${NAME}.${NEWEXT}

echo "Extension: $EXT"
echo "Name: $NAME"
echo "Newname: $NEWNAME"
cheers

Last edited by kbp; 01-13-2010 at 09:34 PM. Reason: typo
 
1 members found this post helpful.
Old 01-13-2010, 09:42 PM   #3
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
courtesy of alunduil

Code:
filename="something.extension"
basename=${filename%.*}
extension=${filename##*.}

Last edited by GooseYArd; 01-13-2010 at 09:45 PM.
 
Old 01-13-2010, 10:40 PM   #4
jdavis2
Member
 
Registered: Feb 2007
Distribution: Fedora, Mandrake, Knoppix, Windows XP
Posts: 37

Original Poster
Rep: Reputation: 15
[SOLVED] Thanks this was exactly what I needed.

Quote:
Originally Posted by kbp View Post
Code:
FILE=$1

FULLNAME=$(basename $FILE)
EXT=$(echo $FULLNAME | sed 's/^.*\.//')
NAME=$(basename $FILE .${EXT})
NEWEXT=mpg
NEWNAME=${NAME}.${NEWEXT}

echo "Extension: $EXT"
echo "Name: $NAME"
echo "Newname: $NEWNAME"
cheers
Here is my final script and it works like a charm. I even managed to understand most of what happened.

#!/bin/sh

FILE=$1

FULLNAME=$(basename $FILE)
EXT=$(echo $FULLNAME | sed 's/^.*\.//')
NAME=$(basename $FILE .${EXT})
NEWEXT=avi
NEWNAME=${NAME}.${NEWEXT}

ffmpeg -i $FILE -r 20 -vcodec mpeg4 -vtag DX50 -b 512k -acodec mp3 -ab 128k -bufsize 4m -qmax 51 -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -ar 44100 -g 300 -s 224x176 -aspect 4:3 -ac 2 -y $NEWNAME
 
Old 01-14-2010, 12:08 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,050

Rep: Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971
There is a lot of redundant processing there - doesn't matter for 1 file, but could in a decent sized loop. As per GooseYArd, what about this ?
Code:
#!/bin/sh

ffmpeg -i $1 -r 20 -vcodec mpeg4 -vtag DX50 -b 512k -acodec mp3 -ab 128k -bufsize 4m -qmax 51 -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -ar 44100 -g 300 -s 224x176 -aspect 4:3 -ac 2 -y ${1%.*}.avi
 
1 members found this post helpful.
Old 01-14-2010, 05:39 PM   #6
jdavis2
Member
 
Registered: Feb 2007
Distribution: Fedora, Mandrake, Knoppix, Windows XP
Posts: 37

Original Poster
Rep: Reputation: 15
That is awesome.

Quote:
Originally Posted by syg00 View Post
There is a lot of redundant processing there - doesn't matter for 1 file, but could in a decent sized loop. As per GooseYArd, what about this ?
Code:
#!/bin/sh

ffmpeg -i $1 -r 20 -vcodec mpeg4 -vtag DX50 -b 512k -acodec mp3 -ab 128k -bufsize 4m -qmax 51 -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -ar 44100 -g 300 -s 224x176 -aspect 4:3 -ac 2 -y ${1%.*}.avi
I wasn't able to following what GooseYArd posted before but now I understand. Thanks for pointing out how simple this could be if you know the rules.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need bash script to list files, drop extension and dump to file talwar_ Programming 10 06-03-2011 09:18 AM
BASH: how to substitute a expression with another without creating a new file carolflb Linux - Newbie 8 11-18-2009 09:42 AM
BASH RegEx file name parsing Hewson Linux - General 7 04-27-2007 05:37 PM
bash shell script expression CowboyJ Programming 2 04-24-2005 11:33 PM
bash script to set extension association vi0lat0r Programming 6 05-24-2004 07:04 PM


All times are GMT -5. The time now is 02:49 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration