LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-14-2009, 10:26 AM   #1
rodhull
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Rep: Reputation: 0
Passing a variable to bc in bash - HELP!


I cannot for the life of me get this little (simple) script I wrote to work.

Here is the entire script:

Code:
#!/bin/bash

ASPECT=`mediainfo $1 |grep "Display aspect ratio" |cut -d : -f 2`
HEIGHT=`echo "320 / $ASPECT" |bc`
SIZE=`echo 320x$HEIGHT`

ffmpeg -i $1 -s $SIZE -f mp4 -vcodec libxvid -maxrate 2500 -qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec libfaac -ab 128k $2
So, here's what should happen:
An input filename ($1) is fed into mediainfo, which by the use of grep and cut spits out a single number which is the aspect ratio. This is then divided by bc into 320, which gives the desired height dimension for the file that I want ffmpeg to create for me. Finally, ffmpeg runs using the calculated dimensions...

Basically, it's the passing of the $ASPECT variable to bc that seems to fail.

It looks like bc won't read the output from the mediainfo line...

It always crashes out with:

Code:
(standard_in) 1: illegal character: ^M
I've tried doing something even simpler like this to debug by just trying it to display the calculation on the screen:

Code:
#!/bin/bash

ASPECT=`mediainfo $1 |grep "Display aspect ratio" |cut -d : -f 2`
HEIGHT=`echo "320 / $ASPECT" |bc`
echo $HEIGHT
...and it does the same, so it's definitely bc that won't accept the output from mediainfo...

Can anyone help?

Last edited by rodhull; 01-14-2009 at 06:59 PM.
 
Old 01-14-2009, 10:45 AM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
I'm sure that others will come along with solutions which are more easily typed than the following, but this one is sure to work in pretty much any distribution of Linux or UNIX:
Code:
ASPECT=`mediainfo $1 |sed -e 's/[^ -~]//g' |grep "Display aspect ratio" |cut -d : -f 2`
                                  ^ ^
                                  | |
                              space tilde
mediainfo probably outputs a carriage return before the line feed at the end of the line, and this filters out the carriage return.

Hope this helps.
 
Old 01-14-2009, 03:06 PM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Not sure it's easier to type but:
Code:
ASPECT=`mediainfo $1 |sed 's/^M//g' |grep "Display aspect ratio" |cut -d : -f 2`
^M in this is a single character created by typing [Ctrl]+[v] followed by [Ctrl]+[m]
 
Old 01-14-2009, 04:17 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
It is the output of media info which is in DOS format, that is each line terminates with a line feed (carriage return + newline). You can filter the output using the dos2unix command:
Code:
mediainfo $1 | grep "Display aspect ratio" | dos2unix | cut -d : -f 2
You can see the difference piping the output to od -c. For example:
Code:
$ mediainfo file.avi | grep "Display aspect ratio" | od -c
0000000   D   i   s   p   l   a   y       a   s   p   e   c   t       r
0000020   a   t   i   o
0000040       :       4   /   3  \r  \n
0000050
$ mediainfo file.avi | grep "Display aspect ratio" | dos2unix | od -c
0000000   D   i   s   p   l   a   y       a   s   p   e   c   t       r
0000020   a   t   i   o
0000040       :       4   /   3  \n
0000047
 
Old 01-14-2009, 06:59 PM   #5
rodhull
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks, guys.

I decided to go with the dos2unix variant in the end. Strange how mediainfo produces a DOS-like output - nevermind!

Here's the full line I used for the HEIGHT var. in the end after a little more experimentation:

Code:
HEIGHT=`echo "scale=3;320/($ASPECT)"|bc|cut -d . -f 1`
Some aspect ratios given by mediainfo are decimal numbers, but others are formats like 4/3 or 16/9 etc. so I had to enclose the $ASPECT var. in brackets. The "scale" option is needed to give a more accurate figure from the calculation (3 dec. places), but then the second "cut" is required to remove the decimal places and provide an integer in pixels for the height for ffmpeg - it obviously won't allow a fraction of a pixel!

The script now converts virtually anything into an iPod-ready video at the original aspect ratio.

Many thanks again!

Last edited by rodhull; 01-14-2009 at 07:02 PM.
 
  


Reply



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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
bash: passing asterisk to variable command Dr_Death_UAE Programming 14 11-26-2008 10:04 AM
passing NameVirtualHost as a variable sunethj Linux - Server 5 05-15-2007 01:51 AM
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 02:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:25 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