5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Paperclipで動画をリサイズする

Last updated at Posted at 2016-02-06

READMEに指定方法が書かれておらず少しハマったのでそのメモ。

例えば、アイコン画像(ここではavator)を幅120pxでアスペクト比を維持したままリサイズするときには PaperClipのstyleで指定する記号の意味 にあるように、

has_attached_file :avatar,
  styles: { 
    thumb: "120x" 
  },
  default_url: "/images/:style/missing.png" 

とすることでアスペクト比を維持したままheightを自動的に計算して変換してくれます。

動画(例えば.mp4)をリサイズする場合には、paperclipのprocessorを拡張する形で提供されている paperclip-av-transcoder と言うgemで実現できます。しかし、普通に

has_attached_file :movie,
  styles: { 
    thumb: ["120x", 'mp4']
  },
  default_url: "/movies/:style/missing.mp4",
  processors: [:transcoder]

とするとうまくいきません。これはpaperclip-av-transcoderでは内部でffmpeg(またはlibav)を使っていてオプションの指定方法が異なるためです。

次のようにすることで -vf "scale=120:-1" の指定ができるようになるので、同様のことが実現できます。

has_attached_file :movie,
  styles: { 
    thumb: {
      format: 'mp4',
      streaming: true,
      convert_options: { 
        output: { vf: "scale=120:-1" }
      }
    },
  },
  default_url: "/movies/:style/missing.mp4",
  processors: [:transcoder]
5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?