我正在尝试使用delayed_job在后台处理大型视频和音频文件。在大多数情况下,一切正常。唯一遇到问题的时候是上传较大的文件(~>200MB)
应用程序/模型/userfile.rb
has_attached_file :userfile,
path: ':dir_path/:style_:filename',
use_timestamp: false, styles: lambda { |a| UserfileStyles.get(a.instance)[:styles] },
only_process: lambda { |a| UserfileStyles.get(a.instance)[:foreground] },
source_file_options: { all: '-auto-orient' }
validates_attachment_content_type :userfile, content_type: /.*/
process_in_background :userfile,
url_with_processing: false,
only_process: lambda { |a| UserfileStyles.get(a.instance)[:background] }
应用程序/模型/userfile_styles.rb
module UserfileStyles
def self.get userfile
if userfile.video?
{
styles: {
screenshot: ['300x300', :jpg],
thumbnail: {
gemoetry: '100x100#',
format: :jpg,
convert_options: '-thumbnail 100%'
},
preview: {
format: 'mp4',
convert_options: {
output: { ss: '0', t: '10' }
},
processors: [:transcoder]
},
mp4: {
format: 'mp4',
convert_options: {
output: {
vcodec: 'libx264',
vb: '1000k',
'profile:v': 'baseline',
vf: 'scale=-2:480',
acodec: 'aac',
ab: '128k',
preset: 'slow',
threads: 0,
movflags: 'faststart',
}
},
processors: [:transcoder]
}
},
foreground: [:screenshot, :thumbnail],
background: [:preview, :mp4]
}
end
end
end
示例(第一个文件正在从第二个文件转换,第三个文件正在从第四个文件转换):
v2@web1 ~/divshare-v2 $ ls -alh /tmp
-rw------- 1 v2 v2 70M Sep 10 00:01 2158940a8739e7219125179e0d1528c120160909-14061-8dqfx020160909-14061-egeyeq.mp4
-rw------- 1 v2 v2 515M Sep 9 23:57 2158940a8739e7219125179e0d1528c120160909-14061-8dqfx0.mp4
-rw------- 1 v2 v2 145M Sep 9 23:33 76ba144beb8a14b6cf542225ef885a7c20160909-12733-1ui03vo20160909-12733-y7ywn.mp4
-rw------- 1 v2 v2 604M Sep 9 23:27 76ba144beb8a14b6cf542225ef885a7c20160909-12733-1ui03vo.mp4
我已经尝试上传几次并使用不同的文件。总是在同一点附近被抓住。然而,当视频较小(~100-200MB)时,一切都很好。
这是正在运行的命令:
v2@web1 ~/divshare-v2 $ ps ux | grep ffmpeg
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
v2 14588 26.4 2.9 849840 240524 ? Sl Sep09 12:00 ffmpeg -i /tmp/2158940a8739e7219125179e0d1528c120160909-14061-8dqfx0.mp4 -acodec aac -strict experimental -vcodec libx264 -vb 1000k -profile:v baseline -vf scale=-2:480 -acodec aac -ab 128k -preset slow -threads 0 -movflags faststart -y /tmp/2158940a8739e7219125179e0d1528c120160909-14061-8dqfx020160909-14061-egeyeq.mp4
任何形式的调试帮助都会很棒。
注意:我复制了上面的命令并手动运行它,以便我可以看到来自 ffmpeg 的一些日志,并且工作完美。