diff --git a/activestorage/lib/active_storage/analyzer/video_analyzer.rb b/activestorage/lib/active_storage/analyzer/video_analyzer.rb index e56c97f4f5..3240705ebd 100644 --- a/activestorage/lib/active_storage/analyzer/video_analyzer.rb +++ b/activestorage/lib/active_storage/analyzer/video_analyzer.rb @@ -44,7 +44,8 @@ module ActiveStorage end def duration - Float(video_stream["duration"]) if video_stream["duration"] + duration = video_stream["duration"] || container["duration"] + Float(duration) if duration end def angle @@ -98,12 +99,22 @@ module ActiveStorage probe["streams"] || [] end + def container + probe["format"] || {} + end + def probe - download_blob_to_tempfile { |file| probe_from(file) } + @probe ||= download_blob_to_tempfile { |file| probe_from(file) } end def probe_from(file) - IO.popen([ ffprobe_path, "-print_format", "json", "-show_streams", "-v", "error", file.path ]) do |output| + IO.popen([ ffprobe_path, + "-print_format", "json", + "-show_streams", + "-show_format", + "-v", "error", + file.path + ]) do |output| JSON.parse(output.read) end rescue Errno::ENOENT diff --git a/activestorage/test/analyzer/video_analyzer_test.rb b/activestorage/test/analyzer/video_analyzer_test.rb index 172a2f0aae..e974c78242 100644 --- a/activestorage/test/analyzer/video_analyzer_test.rb +++ b/activestorage/test/analyzer/video_analyzer_test.rb @@ -45,9 +45,21 @@ class ActiveStorage::Analyzer::VideoAnalyzerTest < ActiveSupport::TestCase assert_nil metadata[:display_aspect_ratio] end + test "analyzing a video with a container-specified duration" do + blob = create_file_blob(filename: "video.webm", content_type: "video/webm") + metadata = extract_metadata_from(blob) + + assert_equal 640, metadata[:width] + assert_equal 480, metadata[:height] + assert_equal 5.229000, metadata[:duration] + end + test "analyzing a video without a video stream" do blob = create_file_blob(filename: "video_without_video_stream.mp4", content_type: "video/mp4") metadata = extract_metadata_from(blob) - assert_equal({ "analyzed" => true, "identified" => true }, metadata) + + assert_not_includes metadata, :width + assert_not_includes metadata, :height + assert_equal 1.022000, metadata[:duration] end end diff --git a/activestorage/test/fixtures/files/video.webm b/activestorage/test/fixtures/files/video.webm new file mode 100644 index 0000000000..ffc29d479e Binary files /dev/null and b/activestorage/test/fixtures/files/video.webm differ