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".
This commit is contained in:
evazion
2022-11-05 01:02:29 -05:00
parent 4a241ac6b5
commit 28237e2e09
4 changed files with 26 additions and 2 deletions

View File

@@ -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

View File

@@ -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) }

View File

@@ -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

View File

@@ -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")