# Ffmpeg + mp3 convert

**URL:** <https://www.ruby-forum.com/t/ffmpeg-mp3-convert/168455>\
**Category:** Rails\
**Created:** [May 20, 2009, 11:47pm UTC](https://www.ruby-forum.com/t/ffmpeg-mp3-convert/168455 "2009-05-20T23:47:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [May 20, 2009, 11:47pm UTC](https://www.ruby-forum.com/t/ffmpeg-mp3-convert/168455/1 "2009-05-20T23:47:48Z")

</div>

Hi all,

I am using paperclip plugin to upload mp3’s. Before I save the mp3 I  
would like to convert it to a smaller size.

I am using ffmpeg library and in my Track model I am calling:

before\_save :convert\_mp3

def convert\_mp3  
system(“ffmpeg -i #{mp3.to\_file.path} -vn -ar 44100 -ac 2 -ab 64 -f  
mp3 #{mp3.to\_file.path}”)  
end

But this fails. Am I missing something?

Thanks Pete

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [May 21, 2009, 3:14am UTC](https://www.ruby-forum.com/t/ffmpeg-mp3-convert/168455/2 "2009-05-21T03:14:43Z")

</div>

Hi Pete,

What exactly is the error message you’re getting when it fails? I may  
be wrong, but I think it’s trying to call ffmpeg without a file path.  
I say that because, based on my rudimentary understanding of Ruby, the  
mp3 object doesn’t exist inside the scope of convert\_mp3.

So instead, try something like this:

private  
def convert\_mp3  
system(“ffmpeg -i #{self.mp3.to\_file.path} -vn -ar 44100 -ac 2 -ab  
64 -f mp3 #{mp3.to\_file.path}”)  
end

(I recommend putting that inside private for security reasons.)

If that doesn’t work, try to copy-paste the command itself into a  
shell, substitute the filename for something you know is there and  
should work, and try that. In other words, try to isolate where the  
problem is - in the code, or in the execution of ffmpeg.

On May 20, 3:47 pm, Petan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [May 21, 2009, 8:24am UTC](https://www.ruby-forum.com/t/ffmpeg-mp3-convert/168455/3 "2009-05-21T08:24:17Z")

</div>

Hi, thx for your suggestions,

but it doesn’t work either. ☹ it always return the message “Whoops!  
File was NOT uploaded. Please try it again.” // message from my tracks  
controller.

def create  
#@track = logged\_in\_user.tracks.build(params[:track])  
@track = Track.new(params[:track])

```
if logged_in_user.tracks << @track
   flash[:notice] = "Successfully uploaded."
       redirect_to :action => :index
  else
  flash[:error] = "Whoops! File was NOT uploaded. Please try it 

```

again."  
render :action =\> :new  
end  
end

Could it be the command itself and its syntax?  
“ffmpeg -i #{self.mp3.to\_file.path} -vn -ar 44100 -ac 2 -ab 16 -f mp3  
#{mp3.to\_file.path}”

Pete

Phoenix R. wrote:

> Hi Pete,
> 
> What exactly is the error message you’re getting when it fails? I may  
> be wrong, but I think it’s trying to call ffmpeg without a file path.  
> I say that because, based on my rudimentary understanding of Ruby, the  
> mp3 object doesn’t exist inside the scope of convert\_mp3.
> 
> So instead, try something like this:
> 
> private  
> def convert\_mp3  
> system(“ffmpeg -i #{self.mp3.to\_file.path} -vn -ar 44100 -ac 2 -ab  
> 64 -f mp3 #{mp3.to\_file.path}”)  
> end
> 
> (I recommend putting that inside private for security reasons.)
> 
> If that doesn’t work, try to copy-paste the command itself into a  
> shell, substitute the filename for something you know is there and  
> should work, and try that. In other words, try to isolate where the  
> problem is - in the code, or in the execution of ffmpeg.
> 
> On May 20, 3:47ï¿½pm, Petan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [May 21, 2009, 6:28pm UTC](https://www.ruby-forum.com/t/ffmpeg-mp3-convert/168455/4 "2009-05-21T18:28:54Z")

</div>

Well, that’s what I was thinking: isolate your ffmpeg command and see  
if it’s working correctly. Get a file that you know ffmpeg can work  
with, then execute that exact same command on your system, but  
substitute the filename for (self.mp3.to\_file.path. So, for example:

[you@yourbox] $ ffmpeg -i original\_file.wav -vn -ar 44100 -ac 2 -ab 16  
-f mp3 new\_mp3.mp3

Where:

- original\_file.wav - test original file for ffmpeg to convert
- new\_mp3.mp3 - what ffmpeg should create as a result of said  
conversion

Also, check permissions on your various directories. Make sure that  
whatever user is running Ruby (i.e. is this running behind Apache with  
Phusion?) has write and execute permissions on whatever directory  
you’re writing the files to.

On May 21, 12:24 am, Petan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
