Skip to content

Commit

Permalink
convert images to avif/support avif, transcode audio to opus
Browse files Browse the repository at this point in the history
  • Loading branch information
Ember-ruby committed Jan 17, 2024
1 parent a6a7509 commit fc80839
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
7 changes: 5 additions & 2 deletions app/javascript/flavours/glitch/features/audio/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -496,15 +496,18 @@ class Audio extends PureComponent {
/>

{(revealed || editable) && <audio
src={src}
ref={this.setAudioRef}
preload={autoPlay ? 'auto' : 'none'}
onPlay={this.handlePlay}
onPause={this.handlePause}
onProgress={this.handleProgress}
onLoadedData={this.handleLoadedData}
crossOrigin='anonymous'
/>}
>
<source src={src.replace("/original/", "/opus/").replace(".mp3", ".webm")} type="audio/webm; codecs=opus"/>

Check failure on line 507 in app/javascript/flavours/glitch/features/audio/index.jsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected usage of doublequote

Check failure on line 507 in app/javascript/flavours/glitch/features/audio/index.jsx

View workflow job for this annotation

GitHub Actions / lint

A space is required before closing bracket
<source src={src} type="audio/mpeg; codecs=mp3"/>

Check failure on line 508 in app/javascript/flavours/glitch/features/audio/index.jsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected usage of doublequote

Check failure on line 508 in app/javascript/flavours/glitch/features/audio/index.jsx

View workflow job for this annotation

GitHub Actions / lint

A space is required before closing bracket
</audio>
}

<canvas
role='button'
Expand Down
37 changes: 26 additions & 11 deletions app/models/media_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class MediaAttachment < ApplicationRecord
MAX_VIDEO_FRAME_RATE = 240
MAX_VIDEO_FRAMES = 144_000 # Approx. 10 minutes at 240 fps

IMAGE_FILE_EXTENSIONS = %w(.jpg .jpeg .png .gif .webp .heic .heif .avif .jxl).freeze
IMAGE_FILE_EXTENSIONS = %w(.jpg .jpeg .png .gif .webp .heic .heif .avif).freeze
VIDEO_FILE_EXTENSIONS = %w(.webm .mp4 .m4v .mov).freeze
AUDIO_FILE_EXTENSIONS = %w(.ogg .oga .mp3 .wav .flac .opus .aac .m4a .3gp .wma).freeze

Expand All @@ -57,8 +57,8 @@ class MediaAttachment < ApplicationRecord
small
).freeze

IMAGE_MIME_TYPES = %w(image/jpeg image/png image/gif image/heic image/heif image/webp image/avif image/jxl).freeze
IMAGE_CONVERTIBLE_MIME_TYPES = %w(image/heic image/heif image/avif image/jxl).freeze
IMAGE_MIME_TYPES = %w(image/jpeg image/png image/gif image/heic image/heif image/webp image/avif).freeze
IMAGE_CONVERTIBLE_MIME_TYPES = %w(image/heic image/heif).freeze
VIDEO_MIME_TYPES = %w(video/webm video/mp4 video/quicktime video/ogg).freeze
VIDEO_CONVERTIBLE_MIME_TYPES = %w(video/quicktime).freeze
AUDIO_MIME_TYPES = %w(audio/wave audio/wav audio/x-wav audio/x-pn-wave audio/vnd.wave audio/ogg audio/vorbis audio/mpeg audio/mp3 audio/webm audio/flac audio/aac audio/m4a audio/x-m4a audio/mp4 audio/3gpp video/x-ms-asf audio/opus).freeze
Expand All @@ -83,12 +83,12 @@ class MediaAttachment < ApplicationRecord

IMAGE_CONVERTED_STYLES = {
original: {
format: 'webp',
content_type: 'image/webp',
format: 'avif',
content_type: 'image/avif',
}.merge(IMAGE_STYLES[:original]).freeze,

small: {
format: 'webp',
format: 'avif',
}.merge(IMAGE_STYLES[:small]).freeze,
}.freeze

Expand All @@ -98,14 +98,15 @@ class MediaAttachment < ApplicationRecord
vfr_frame_rate_threshold: MAX_VIDEO_FRAME_RATE,
convert_options: {
output: {
'loglevel' => 'fatal',
'loglevel' => 'error',
'preset' => 'veryfast',
'movflags' => 'faststart', # Move metadata to start of file so playback can begin before download finishes
'pix_fmt' => 'yuv420p', # Ensure color space for cross-browser compatibility
'vf' => 'crop=floor(iw/2)*2:floor(ih/2)*2', # h264 requires width and height to be even. Crop instead of scale to avoid blurring
'c:v' => 'h264',
'c:a' => 'aac',
'c:a' => 'opus',
'b:a' => '192k',
'crf' => 18,
'map_metadata' => '-1',
'frames:v' => MAX_VIDEO_FRAMES,
}.freeze,
Expand All @@ -120,7 +121,7 @@ class MediaAttachment < ApplicationRecord
format: 'mp4',
convert_options: {
output: {
'loglevel' => 'fatal',
'loglevel' => 'error',
'map_metadata' => '-1',
'c:v' => 'copy',
'c:a' => 'copy',
Expand Down Expand Up @@ -152,13 +153,27 @@ class MediaAttachment < ApplicationRecord
content_type: 'audio/mpeg',
convert_options: {
output: {
'loglevel' => 'fatal',
'q:a' => 2,
'loglevel' => 'error',
'q:a' => 4,
'map' => 'a',
}.freeze,
}.freeze,
}.freeze,
opus: {
format: 'webm',
content_type: 'audio/webm',
convert_options: {
output: {
'loglevel' => 'error',
'b:a' => '96k',
'c:a' => 'libopus',
'map' => 'a',
}.freeze,
}.freeze,
}.freeze,
}.freeze


Check failure on line 176 in app/models/media_attachment.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/EmptyLines: Extra blank line detected. (https://rubystyle.guide#two-or-more-empty-lines)
VIDEO_CONVERTED_STYLES = {
small: VIDEO_STYLES[:small].freeze,
original: VIDEO_FORMAT.freeze,
Expand Down

0 comments on commit fc80839

Please sign in to comment.