]> cat aescling's git repositories - mastodon.git/blob - app/models/media_attachment.rb
add loglevel fatal to video and audio styles (#12088)
[mastodon.git] / app / models / media_attachment.rb
1 # frozen_string_literal: true
2 # == Schema Information
3 #
4 # Table name: media_attachments
5 #
6 # id :bigint(8) not null, primary key
7 # status_id :bigint(8)
8 # file_file_name :string
9 # file_content_type :string
10 # file_file_size :integer
11 # file_updated_at :datetime
12 # remote_url :string default(""), not null
13 # created_at :datetime not null
14 # updated_at :datetime not null
15 # shortcode :string
16 # type :integer default("image"), not null
17 # file_meta :json
18 # account_id :bigint(8)
19 # description :text
20 # scheduled_status_id :bigint(8)
21 # blurhash :string
22 #
23
24 class MediaAttachment < ApplicationRecord
25 self.inheritance_column = nil
26
27 enum type: [:image, :gifv, :video, :unknown, :audio]
28
29 IMAGE_FILE_EXTENSIONS = %w(.jpg .jpeg .png .gif).freeze
30 VIDEO_FILE_EXTENSIONS = %w(.webm .mp4 .m4v .mov).freeze
31 AUDIO_FILE_EXTENSIONS = %w(.ogg .oga .mp3 .wav .flac .opus .aac .m4a .3gp .wma).freeze
32
33 IMAGE_MIME_TYPES = %w(image/jpeg image/png image/gif).freeze
34 VIDEO_MIME_TYPES = %w(video/webm video/mp4 video/quicktime video/ogg).freeze
35 VIDEO_CONVERTIBLE_MIME_TYPES = %w(video/webm video/quicktime).freeze
36 AUDIO_MIME_TYPES = %w(audio/wave audio/wav audio/x-wav audio/x-pn-wave audio/ogg audio/mpeg audio/mp3 audio/webm audio/flac audio/aac audio/m4a audio/x-m4a audio/mp4 audio/3gpp video/x-ms-asf).freeze
37
38 BLURHASH_OPTIONS = {
39 x_comp: 4,
40 y_comp: 4,
41 }.freeze
42
43 IMAGE_STYLES = {
44 original: {
45 pixels: 1_638_400, # 1280x1280px
46 file_geometry_parser: FastGeometryParser,
47 },
48
49 small: {
50 pixels: 160_000, # 400x400px
51 file_geometry_parser: FastGeometryParser,
52 blurhash: BLURHASH_OPTIONS,
53 },
54 }.freeze
55
56 VIDEO_STYLES = {
57 small: {
58 convert_options: {
59 output: {
60 'loglevel' => 'fatal',
61 vf: 'scale=\'min(400\, iw):min(400\, ih)\':force_original_aspect_ratio=decrease',
62 },
63 },
64 format: 'png',
65 time: 0,
66 file_geometry_parser: FastGeometryParser,
67 blurhash: BLURHASH_OPTIONS,
68 },
69
70 original: {
71 keep_same_format: true,
72 convert_options: {
73 output: {
74 'loglevel' => 'fatal',
75 'map_metadata' => '-1',
76 'c:v' => 'copy',
77 'c:a' => 'copy',
78 },
79 },
80 },
81 }.freeze
82
83 AUDIO_STYLES = {
84 original: {
85 format: 'mp3',
86 content_type: 'audio/mpeg',
87 convert_options: {
88 output: {
89 'loglevel' => 'fatal',
90 'q:a' => 2,
91 },
92 },
93 },
94 }.freeze
95
96 VIDEO_FORMAT = {
97 format: 'mp4',
98 content_type: 'video/mp4',
99 convert_options: {
100 output: {
101 'loglevel' => 'fatal',
102 'movflags' => 'faststart',
103 'pix_fmt' => 'yuv420p',
104 'vf' => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'',
105 'vsync' => 'cfr',
106 'c:v' => 'h264',
107 'maxrate' => '1300K',
108 'bufsize' => '1300K',
109 'frames:v' => 60 * 60 * 3,
110 'crf' => 18,
111 'map_metadata' => '-1',
112 },
113 },
114 }.freeze
115
116 VIDEO_CONVERTED_STYLES = {
117 small: VIDEO_STYLES[:small],
118 original: VIDEO_FORMAT,
119 }.freeze
120
121 IMAGE_LIMIT = 10.megabytes
122 VIDEO_LIMIT = 40.megabytes
123
124 belongs_to :account, inverse_of: :media_attachments, optional: true
125 belongs_to :status, inverse_of: :media_attachments, optional: true
126 belongs_to :scheduled_status, inverse_of: :media_attachments, optional: true
127
128 has_attached_file :file,
129 styles: ->(f) { file_styles f },
130 processors: ->(f) { file_processors f },
131 convert_options: { all: '-quality 90 -strip +set modify-date +set create-date' }
132
133 validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES + AUDIO_MIME_TYPES
134 validates_attachment_size :file, less_than: IMAGE_LIMIT, unless: :larger_media_format?
135 validates_attachment_size :file, less_than: VIDEO_LIMIT, if: :larger_media_format?
136 remotable_attachment :file, VIDEO_LIMIT, suppress_errors: false
137
138 include Attachmentable
139
140 validates :account, presence: true
141 validates :description, length: { maximum: 1_500 }, if: :local?
142
143 scope :attached, -> { where.not(status_id: nil).or(where.not(scheduled_status_id: nil)) }
144 scope :unattached, -> { where(status_id: nil, scheduled_status_id: nil) }
145 scope :local, -> { where(remote_url: '') }
146 scope :remote, -> { where.not(remote_url: '') }
147 scope :cached, -> { remote.where.not(file_file_name: nil) }
148
149 default_scope { order(id: :asc) }
150
151 def local?
152 remote_url.blank?
153 end
154
155 def needs_redownload?
156 file.blank? && remote_url.present?
157 end
158
159 def larger_media_format?
160 video? || gifv? || audio?
161 end
162
163 def audio_or_video?
164 audio? || video?
165 end
166
167 def to_param
168 shortcode
169 end
170
171 def focus=(point)
172 return if point.blank?
173
174 x, y = (point.is_a?(Enumerable) ? point : point.split(',')).map(&:to_f)
175
176 meta = file.instance_read(:meta) || {}
177 meta['focus'] = { 'x' => x, 'y' => y }
178
179 file.instance_write(:meta, meta)
180 end
181
182 def focus
183 x = file.meta['focus']['x']
184 y = file.meta['focus']['y']
185
186 "#{x},#{y}"
187 end
188
189 after_commit :reset_parent_cache, on: :update
190 before_create :prepare_description, unless: :local?
191 before_create :set_shortcode
192 before_post_process :set_type_and_extension
193 before_save :set_meta
194
195 class << self
196 def supported_mime_types
197 IMAGE_MIME_TYPES + VIDEO_MIME_TYPES + AUDIO_MIME_TYPES
198 end
199
200 def supported_file_extensions
201 IMAGE_FILE_EXTENSIONS + VIDEO_FILE_EXTENSIONS + AUDIO_FILE_EXTENSIONS
202 end
203
204 private
205
206 def file_styles(f)
207 if f.instance.file_content_type == 'image/gif' || VIDEO_CONVERTIBLE_MIME_TYPES.include?(f.instance.file_content_type)
208 VIDEO_CONVERTED_STYLES
209 elsif IMAGE_MIME_TYPES.include?(f.instance.file_content_type)
210 IMAGE_STYLES
211 elsif VIDEO_MIME_TYPES.include?(f.instance.file_content_type)
212 VIDEO_STYLES
213 else
214 AUDIO_STYLES
215 end
216 end
217
218 def file_processors(f)
219 if f.file_content_type == 'image/gif'
220 [:gif_transcoder, :blurhash_transcoder]
221 elsif VIDEO_MIME_TYPES.include?(f.file_content_type)
222 [:video_transcoder, :blurhash_transcoder, :type_corrector]
223 elsif AUDIO_MIME_TYPES.include?(f.file_content_type)
224 [:transcoder, :type_corrector]
225 else
226 [:lazy_thumbnail, :blurhash_transcoder, :type_corrector]
227 end
228 end
229 end
230
231 private
232
233 def set_shortcode
234 self.type = :unknown if file.blank? && !type_changed?
235
236 return unless local?
237
238 loop do
239 self.shortcode = SecureRandom.urlsafe_base64(14)
240 break if MediaAttachment.find_by(shortcode: shortcode).nil?
241 end
242 end
243
244 def prepare_description
245 self.description = description.strip[0...420] unless description.nil?
246 end
247
248 def set_type_and_extension
249 self.type = begin
250 if VIDEO_MIME_TYPES.include?(file_content_type)
251 :video
252 elsif AUDIO_MIME_TYPES.include?(file_content_type)
253 :audio
254 else
255 :image
256 end
257 end
258 end
259
260 def set_meta
261 meta = populate_meta
262
263 return if meta == {}
264
265 file.instance_write :meta, meta
266 end
267
268 def populate_meta
269 meta = file.instance_read(:meta) || {}
270
271 file.queued_for_write.each do |style, file|
272 meta[style] = style == :small || image? ? image_geometry(file) : video_metadata(file)
273 end
274
275 meta
276 end
277
278 def image_geometry(file)
279 width, height = FastImage.size(file.path)
280
281 return {} if width.nil?
282
283 {
284 width: width,
285 height: height,
286 size: "#{width}x#{height}",
287 aspect: width.to_f / height.to_f,
288 }
289 end
290
291 def video_metadata(file)
292 movie = FFMPEG::Movie.new(file.path)
293
294 return {} unless movie.valid?
295
296 {
297 width: movie.width,
298 height: movie.height,
299 frame_rate: movie.frame_rate,
300 duration: movie.duration,
301 bitrate: movie.bitrate,
302 }.compact
303 end
304
305 def reset_parent_cache
306 return if status_id.nil?
307
308 Rails.cache.delete("statuses/#{status_id}")
309 end
310 end
This page took 0.14327 seconds and 5 git commands to generate.