From 28237e2e09479c72ee3ef5a2ec105c5a28266d6d Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 5 Nov 2022 01:02:29 -0500 Subject: [PATCH] posts: automatically tag videos with sound. Automatically add the `sound` tag if the post has sound. Remove the tag if the post doesn't have sound. A video is considered to have sound if its peak loudness is greater than -70 dB. The current quietest post on Danbooru has a peak loudness of -62 dB (post #3470668), but it's possible to have audible sound at -80 dB or possibly even lower. It's hard to draw a clear line between "silent" and "barely audible". --- app/logical/exif_tool.rb | 5 +++++ app/models/media_asset.rb | 2 +- app/models/post.rb | 3 ++- test/unit/post_test.rb | 18 ++++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/logical/exif_tool.rb b/app/logical/exif_tool.rb index bddda7716..48ba1aac5 100644 --- a/app/logical/exif_tool.rb +++ b/app/logical/exif_tool.rb @@ -124,6 +124,11 @@ class ExifTool metadata.has_key?("PNG:Dream") end + # True if the video has audible sound. False if the video doesn't have an audio track, or the audio track is inaudible. + def has_sound? + metadata["FFmpeg:AudioPeakLoudness"].to_f >= 0.0003 # -70 dB + end + def width metadata.find { |name, value| name.match?(/\A(File|PNG|GIF|RIFF|Flash|Track\d+):ImageWidth\z/) }&.second end diff --git a/app/models/media_asset.rb b/app/models/media_asset.rb index 14a38085a..a09576efd 100644 --- a/app/models/media_asset.rb +++ b/app/models/media_asset.rb @@ -22,7 +22,7 @@ class MediaAsset < ApplicationRecord has_many :ai_tags delegate :frame_delays, :metadata, to: :media_metadata - delegate :is_non_repeating_animation?, :is_greyscale?, :is_rotated?, :is_ai_generated?, to: :metadata + delegate :is_non_repeating_animation?, :is_greyscale?, :is_rotated?, :is_ai_generated?, :has_sound?, to: :metadata scope :public_only, -> { where(is_public: true) } scope :private_only, -> { where(is_public: false) } diff --git a/app/models/post.rb b/app/models/post.rb index 65d218ffc..31b994ba6 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -402,7 +402,7 @@ class Post < ApplicationRecord end def add_automatic_tags(tags) - tags -= %w[incredibly_absurdres absurdres highres lowres flash video ugoira animated_gif animated_png exif_rotation non-repeating_animation non-web_source wide_image tall_image] + tags -= %w[incredibly_absurdres absurdres highres lowres flash video ugoira animated_gif animated_png exif_rotation non-repeating_animation non-web_source wide_image tall_image sound] if tags.size >= 30 tags -= ["tagme"] @@ -472,6 +472,7 @@ class Post < ApplicationRecord tags << "exif_rotation" if media_asset.is_rotated? tags << "non-repeating_animation" if media_asset.is_non_repeating_animation? tags << "ai-generated" if media_asset.is_ai_generated? + tags << "sound" if media_asset.has_sound? tags end diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index e37a484ba..3a2fa57d0 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -1264,6 +1264,24 @@ class PostTest < ActiveSupport::TestCase end end + context "a silent video with the sound tag" do + should "automatically remove the sound tag" do + @media_asset = MediaAsset.upload!("test/files/mp4/test-silent-audio.mp4") + @post.update!(md5: @media_asset.md5) + @post.reload.update!(tag_string: "sound") + assert_equal("animated tagme", @post.tag_string) + end + end + + context "an audible video without the sound tag" do + should "automatically add the sound tag" do + @media_asset = MediaAsset.upload!("test/files/mp4/test-audio.mp4") + @post.update!(md5: @media_asset.md5) + @post.reload.update!(tag_string: "tagme") + assert_equal("animated sound tagme", @post.tag_string) + end + end + context "a post with a non-web source" do should "automatically add the non-web_source tag" do @post.update!(source: "this was once revealed to me in a dream")