Skip to content

Commit

Permalink
Audio uploads feature
Browse files Browse the repository at this point in the history
  • Loading branch information
DJSundog authored and nolanlawson committed May 4, 2019
1 parent 2e3b91b commit 66bfaf3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
28 changes: 25 additions & 3 deletions app/models/media_attachment.rb
Expand Up @@ -24,14 +24,16 @@
class MediaAttachment < ApplicationRecord
self.inheritance_column = nil

enum type: [:image, :gifv, :video, :unknown]
enum type: [:image, :gifv, :video, :audio, :unknown]

IMAGE_FILE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif', '.webp'].freeze
VIDEO_FILE_EXTENSIONS = ['.webm', '.mp4', '.m4v', '.mov'].freeze
AUDIO_FILE_EXTENSIONS = ['.mp3', '.m4a', '.wav', '.ogg'].freeze

IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
VIDEO_MIME_TYPES = ['video/webm', 'video/mp4', 'video/quicktime'].freeze
VIDEO_CONVERTIBLE_MIME_TYPES = ['video/webm', 'video/quicktime'].freeze
AUDIO_MIME_TYPES = ['audio/mpeg', 'audio/mp4', 'audio/vnd.wav', 'audio/wav', 'audio/x-wav', 'audio/x-wave', 'audio/ogg',].freeze

BLURHASH_OPTIONS = {
x_comp: 4,
Expand All @@ -51,6 +53,22 @@ class MediaAttachment < ApplicationRecord
},
}.freeze

AUDIO_STYLES = {
original: {
format: 'mp4',
convert_options: {
output: {
filter_complex: '"[0:a]compand,showwaves=s=640x360:mode=line,format=yuv420p[v]"',
map: '"[v]" -map 0:a',
threads: 2,
vcodec: 'libx264',
acodec: 'aac',
movflags: '+faststart',
},
},
},
}.freeze

VIDEO_STYLES = {
small: {
convert_options: {
Expand Down Expand Up @@ -95,7 +113,7 @@ class MediaAttachment < ApplicationRecord
processors: ->(f) { file_processors f },
convert_options: { all: '-quality 90 -strip' }

validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES
validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES + AUDIO_MIME_TYPES
validates_attachment_size :file, less_than: IMAGE_LIMIT, unless: :video_or_gifv?
validates_attachment_size :file, less_than: VIDEO_LIMIT, if: :video_or_gifv?
remotable_attachment :file, VIDEO_LIMIT
Expand Down Expand Up @@ -163,6 +181,8 @@ def file_styles(f)
}
elsif IMAGE_MIME_TYPES.include? f.instance.file_content_type
IMAGE_STYLES
elsif AUDIO_MIME_TYPES.include? f.instance.file_content_type
AUDIO_STYLES
elsif VIDEO_CONVERTIBLE_MIME_TYPES.include?(f.instance.file_content_type)
{
small: VIDEO_STYLES[:small],
Expand All @@ -178,6 +198,8 @@ def file_processors(f)
[:gif_transcoder, :blurhash_transcoder]
elsif VIDEO_MIME_TYPES.include? f.file_content_type
[:video_transcoder, :blurhash_transcoder]
elsif AUDIO_MIME_TYPES.include? f.file_content_type
[:audio_transcoder, :blurhash_transcoder]
else
[:lazy_thumbnail, :blurhash_transcoder]
end
Expand All @@ -202,7 +224,7 @@ def prepare_description
end

def set_type_and_extension
self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : AUDIO_MIME_TYPES.include?(file_content_type) ? :audio : :image
end

def set_meta
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/initial_state_serializer.rb
Expand Up @@ -59,7 +59,7 @@ def accounts
end

def media_attachments
{ accept_content_types: MediaAttachment::IMAGE_FILE_EXTENSIONS + MediaAttachment::VIDEO_FILE_EXTENSIONS + MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES }
{ accept_content_types: MediaAttachment::IMAGE_FILE_EXTENSIONS + MediaAttachment::VIDEO_FILE_EXTENSIONS + MediaAttachment::AUDIO_FILE_EXTENSIONS + MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES + MediaAttachment::AUDIO_MIME_TYPES }
end

private
Expand Down
1 change: 1 addition & 0 deletions config/application.rb
Expand Up @@ -10,6 +10,7 @@
require_relative '../lib/paperclip/lazy_thumbnail'
require_relative '../lib/paperclip/gif_transcoder'
require_relative '../lib/paperclip/video_transcoder'
require_relative '../lib/paperclip/audio_transcoder'
require_relative '../lib/mastodon/snowflake'
require_relative '../lib/mastodon/version'
require_relative '../lib/devise/ldap_authenticatable'
Expand Down
21 changes: 21 additions & 0 deletions lib/paperclip/audio_transcoder.rb
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Paperclip
class AudioTranscoder < Paperclip::Processor
def make
meta = ::Av.cli.identify(@file.path)
# {:length=>"0:00:02.14", :duration=>2.14, :audio_encode=>"mp3", :audio_bitrate=>"44100 Hz", :audio_channels=>"mono"}
if meta[:duration] > 300.0
raise Mastodon::ValidationError, "Audio uploads must be less than 5 minutes in length."
end

final_file = Paperclip::Transcoder.make(file, options, attachment)

attachment.instance.file_file_name = 'media.mp4'
attachment.instance.file_content_type = 'video/mp4'
attachment.instance.type = MediaAttachment.types[:video]

final_file
end
end
end

0 comments on commit 66bfaf3

Please sign in to comment.