在bash中检测视频是否黑白

问题描述 投票:0回答:2

我有一个文件夹,里面有数百张胶片,我想将彩色胶片与黑白胶片分开。有没有 bash 命令可以对一般视频文件执行此操作?

我已经提取了一帧:

ffmpeg -ss 00:15:00 -i vid.mp4 -t 1 -r 1/1 image.bmp

如何检查图像是否有颜色成分?

bash video
2个回答
2
投票

我从来没有发现为什么视频处理问题会在 SO 上得到解答,但由于它们通常不会关闭,我会尽力而为...由于这是一个开发板,我不能推荐任何现成的命令行工具用于您的 bash 命令,我也不知道。另外,我无法给出仅 bash 的解决方案,因为我不知道如何在 bash 中处理二进制数据。

要确定图像是否是灰色的,您需要检查每个像素的颜色并“猜测”它是否是灰色的。正如其他人在评论中所说,您需要分析每个视频的多张图片才能获得更准确的结果。为此,您可以使用 ffmpeg 的场景变化检测过滤器,但那是另一个主题。

我首先调整图像大小以节省处理能力,例如至 4x4 像素。还要确保您保证色彩空间或更好的 pix_format 是已知的,以便您知道像素的样子。

使用此 ffmpeg 行,您可以将 4x4 像素的一帧提取为原始 RGB24:

ffmpeg -i D:\smpte.mxf -pix_fmt rgb24 -t 1 -r 1/1 -vf scale=4:4 -f rawvideo d:\out_color.raw

生成的文件正好包含 48 个字节,每 3 个字节 16 个像素,代表 R、G、B 颜色。要检查是否所有像素都是灰色的,您需要比较 R G 和 B 之间的差异。通常,当它们是灰色时,R G 和 B 具有相同的值,但实际上您需要允许一些更模糊的匹配,例如如果所有值都相同 +-10。

一些 Perl 代码示例:

use strict;
use warnings;

my $fuzz = 10;
my $inputfile ="d:\\out_grey.raw";
die "input file is not an RGB24 raw picture." if ( (-s $inputfile) %3 != 0);
open (my $fh,$inputfile);
binmode $fh;

my $colordetected = 0;

for (my $i=0;$i< -s $inputfile;$i+=3){
    my ($R,$G,$B);
    read ($fh,$R,1);
    $R = ord($R);
    read ($fh,$B,1);
    $B = ord($B);
    read ($fh,$G,1);
    $G = ord($G);
     if ( $R >= $B-$fuzz  and  $R <= $B+$fuzz and  $B >= $G-$fuzz and  $B <= $G+$fuzz )  {
       #this pixel seems gray
     }else{
        $colordetected ++,
     } 
}

if ($colordetected != 0){
    print "There seem to be colors in this image"
}

0
投票

我参加聚会可能会迟到,但这对我有用。如果需要,将独特颜色测试计数从 10 修改为适合您自己的视频。假设中心帧代表整体视频颜色/黑白情况。

#!/bin/bash

mkdir -p BW COL temp_frames

for video in *.mp4 *.m4v; do
    if [[ -f "$video" ]]; then # Extract a frame from the middle of the video using ffmpeg
    ffmpeg -loglevel error -i "$video" -ss "$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$video" | awk '{print int($1/2)}')" -vframes 1 -q:v 2 -f image2 -update 1 "temp_frames/test_frame.jpg"
    count=$(convert temp_frames/test_frame.jpg -unique-colors - | wc -l)

        if [ "$count" -gt 10 ]; then
        echo "Moving color video: $video to COL/"
        \gmv -b "$video" COL/
    else
        echo "$video has $count unique colours"
        echo "Moving black and white video: $video to BW/"
        \gmv -b "$video" BW/
    fi

    # Clean up temp frames
    \rm -f temp_frames/*
    fi
done

© www.soinside.com 2019 - 2024. All rights reserved.