# How to upload mp3 with custom duration/length

**URL:** <https://www.ruby-forum.com/t/how-to-upload-mp3-with-custom-duration-length/195906>\
**Category:** Rails\
**Created:** [October 2, 2010, 3:08pm UTC](https://www.ruby-forum.com/t/how-to-upload-mp3-with-custom-duration-length/195906 "2010-10-02T15:08:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zurich\_by\_night](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zurich\_by\_night](https://www.ruby-forum.com/u/zurich_by_night)\
**Post date:** [October 2, 2010, 3:08pm UTC](https://www.ruby-forum.com/t/how-to-upload-mp3-with-custom-duration-length/195906/1 "2010-10-02T15:08:23Z")

</div>

Does anyone know the right approach to change the duration/length of  
an uploaded mp3 file? User has the option to upload mp3 file as full,  
30 secs or 90 secs. So far, I have gotten it to work for full.

Do I have to use a processor (with ffmpeg) for this? Or is there a  
built-in option in paperclip for this to happen?

Tried googling around to no avail…

By the way, if user selects 90 secs (for example), then I need  
Paperclip not to upload the original. The same goes for 30 secs. In  
theory, its one file or the other.

Your suggestions / feedback are welcome

---

<div class="post-metadata">

**Author:** ![zurich\_by\_night](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zurich\_by\_night](https://www.ruby-forum.com/u/zurich_by_night)\
**Post date:** [October 2, 2010, 3:09pm UTC](https://www.ruby-forum.com/t/how-to-upload-mp3-with-custom-duration-length/195906/2 "2010-10-02T15:09:33Z")

</div>

Note that I am using Paperclip for all my uploads …

On Oct 2, 9:07 pm, Christian F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![zurich\_by\_night](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zurich\_by\_night](https://www.ruby-forum.com/u/zurich_by_night)\
**Post date:** [October 2, 2010, 3:41pm UTC](https://www.ruby-forum.com/t/how-to-upload-mp3-with-custom-duration-length/195906/3 "2010-10-02T15:41:09Z")

</div>

This is what I have. When i submit the form, the page just hangs. No  
errors, no nothing.

model:  
has\_attached\_file :media,  
:styles =\> { :short =\> ‘k128’ },  
:url =\> ‘/assets/:id/:style.:extension’,  
:path =\> ‘:rails\_root/public/  
assets/:id/:style.:extension’,  
:processors =\> [:process\_audio]

* * *

module Paperclip  
class ProcessAudio \< Processor

```
attr_accessor :resolution, :whiny

def initialize(file, options = {}, attachment = nil)
  super
  @file = file
  @whiny = options[:whiny].nil? ? true : options[:whiny]
  @basename = File.basename(@file.path, File.extname(@file.path))
end

def make
  dst = Tempfile.new([@basename, 'mp3'].compact.join("."))
  dst.binmode

  cmd = "ffmpeg -i #{@file.path} -t 30 -acodec copy

```

#{File.expand\_path(dst.path)}"

```
  begin
    success = Paperclip.run(cmd, [0, 1])
  rescue PaperclipCommandLineError
    raise PaperclipError, "There was an error processing the

```

preview for #{@basename}" if whiny  
end  
dst  
end  
end  
end

On Oct 2, 9:08 pm, Christian F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![zurich\_by\_night](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zurich\_by\_night](https://www.ruby-forum.com/u/zurich_by_night)\
**Post date:** [October 2, 2010, 9:07pm UTC](https://www.ruby-forum.com/t/how-to-upload-mp3-with-custom-duration-length/195906/4 "2010-10-02T21:07:10Z")

</div>

I got mine to work with the following code, incase future devs stumble  
across a similar situation:

module Paperclip  
class ProcessAudio \< Processor

```
attr_accessor :geometry, :whiny

def initialize(file, options = {}, attachment = nil)
  super
  @file = file

  unless options[:geometry].nil?
    if options[:geometry] == 'full'
      @geometry = ''
    elsif options[:geometry] == '30'
      @geometry = '-t 30'
    elsif options[:geometry] == '90'
      @geometry = '-t 90'
    end
  end

  @whiny = options[:whiny].nil? ? true : options[:whiny]
  @basename = File.basename(@file.path, File.extname(@file.path))
end

def make
  dst = Tempfile.new([@basename, 'mp3'].compact.join("."))
  dst.binmode

  cmd = "ffmpeg -i #{@file.path} #{@geometry} -acodec copy -y

```

#{File.expand\_path(dst.path)}"

```
  begin
    success = Paperclip.run(cmd)
  rescue PaperclipCommandLineError
    raise PaperclipError, "There was an error processing the

```

preview for #{@basename}" if whiny  
end  
dst  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![zurich\_by\_night](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zurich\_by\_night](https://www.ruby-forum.com/u/zurich_by_night)\
**Post date:** [October 2, 2010, 7:14pm UTC](https://www.ruby-forum.com/t/how-to-upload-mp3-with-custom-duration-length/195906/5 "2010-10-02T19:14:48Z")

</div>

Checkout  
[http://blog.ncodedevlabs.com/2010/01/14/extracting-audio-length-using-rails/](http://blog.ncodedevlabs.com/2010/01/14/extracting-audio-length-using-rails/)

On Oct 2, 1:39 pm, Christian F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
