简体   繁体   中英

Crop Video and Audio files when uploading in a Rails app using paperclip

I want to crop the audio and video based on length to show a sample of about 30 seconds along with saving the original audio or video file.

I am using this gem in Rails Paperclip-FFMPEG to generate the thumbnail image for videos.

But I also want to crop the video to a max length and also generate a sample audio for the audio by taking the first 30 seconds of the audio.

I looked at the docs but was not able to find any documentations or questions like this on Stackoverflow.

Does anyone know a solution on how to do this with paperclip-ffmpeg or another gem?

Thanks in advance.

With paper clip you should use the following command of ffmpeg to generate the crop audio/video:

ffmpeg -ss 0 -i file.mp3 -t 20 file.wav

Look at the -t and -ss arguments, it will do what you want:

-t duration

Restrict the transcoded/captured video sequence to the duration specified in seconds. hh:mm:ss[.xxx] syntax is also supported.

-ss position

Seek to given time position in seconds. hh:mm:ss[.xxx] syntax is also supported.

For example, ffmpeg -ss 0 -t 20 -i inputfile.mp3 -acodec copy outputfile.mp3

It's start the video / audio from 0-20 seconds

-ss 0 - Start at 0 seconds
-t 30 - Capture 30 seconds (from 0, so 0:00 - 0:30). If you want 1 minute of audio, use -t 60.
-acodec copy - Stream copy (re-mux) the audio instead of re-encode it.
file.mp3 - Input file
file.wav - output file

Hope this will solve your problem.

I converted the ffmpeg commands above into a paperclip extension, hope this's useful.

https://github.com/jentzheng/paperclip_audio_crop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM