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:
@@ -124,6 +124,11 @@ class ExifTool
|
|||||||
metadata.has_key?("PNG:Dream")
|
metadata.has_key?("PNG:Dream")
|
||||||
end
|
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
|
def width
|
||||||
metadata.find { |name, value| name.match?(/\A(File|PNG|GIF|RIFF|Flash|Track\d+):ImageWidth\z/) }&.second
|
metadata.find { |name, value| name.match?(/\A(File|PNG|GIF|RIFF|Flash|Track\d+):ImageWidth\z/) }&.second
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ class MediaAsset < ApplicationRecord
|
|||||||
has_many :ai_tags
|
has_many :ai_tags
|
||||||
|
|
||||||
delegate :frame_delays, :metadata, to: :media_metadata
|
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 :public_only, -> { where(is_public: true) }
|
||||||
scope :private_only, -> { where(is_public: false) }
|
scope :private_only, -> { where(is_public: false) }
|
||||||
|
|||||||
@@ -402,7 +402,7 @@ class Post < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def add_automatic_tags(tags)
|
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
|
if tags.size >= 30
|
||||||
tags -= ["tagme"]
|
tags -= ["tagme"]
|
||||||
@@ -472,6 +472,7 @@ class Post < ApplicationRecord
|
|||||||
tags << "exif_rotation" if media_asset.is_rotated?
|
tags << "exif_rotation" if media_asset.is_rotated?
|
||||||
tags << "non-repeating_animation" if media_asset.is_non_repeating_animation?
|
tags << "non-repeating_animation" if media_asset.is_non_repeating_animation?
|
||||||
tags << "ai-generated" if media_asset.is_ai_generated?
|
tags << "ai-generated" if media_asset.is_ai_generated?
|
||||||
|
tags << "sound" if media_asset.has_sound?
|
||||||
|
|
||||||
tags
|
tags
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1264,6 +1264,24 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
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
|
context "a post with a non-web source" do
|
||||||
should "automatically add the non-web_source tag" do
|
should "automatically add the non-web_source tag" do
|
||||||
@post.update!(source: "this was once revealed to me in a dream")
|
@post.update!(source: "this was once revealed to me in a dream")
|
||||||
|
|||||||
Reference in New Issue
Block a user