每当音量为零时,如何用ffmpeg分割mp4视频? [英] How can I split an mp4 video with ffmpeg every time the volume is zero?

查看:185
本文介绍了每当音量为零时,如何用ffmpeg分割mp4视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将视频分割成许多较小的视频. 我尝试过PySceneDetect,它的2种场景检测方法不符合我的需求.

I need to split a video into many smaller videos. I have tried PySceneDetect and its 2 scene detection methods don't fit my need.

这个想法是,每当音量非常低,每次音频电平小于给定参数时,都会触发场景切换/中断.我认为我的意思是总体RMS dB音量水平.

The idea is to trigger a scene cut/break every time the volume is very low, every time audio level is less than a given parameter. I think overall RMS dB volume level is what I mean.

目的是将mp4视频拆分为许多简短的视频,每个较小的视频都带有简短的对话短语.

The purpose is to split an mp4 video into many short videos, each smaller video with short dialog phrases.

到目前为止,我有一个命令来获取总体RMS音频音量.

So far I have a command to get the overall RMS audio volume level.

ffprobe -f lavfi -i amovie=01x01TheStrongestMan.mp4,astats=metadata=1:reset=1 -show_entries frame=pkt_pts_time:frame_tags=lavfi.astats.Overall.RMS_level,lavfi.astats.1.RMS_level,lavfi.astats.2.RMS_level -of csv=p=0

如何仅获取RMS电平及其对应帧或时间的最小值?

How can I get only the minimum values for RMS level and its corresponding frame or time?

然后我如何使用ffmpeg在与最小RMS相对应的每一帧上将视频分割成许多视频?

And then how can I use ffmpeg to split the video in many videos on every frame that corresponds to a minimum RMS?

谢谢.

推荐答案

使用silencedetect音频过滤器,并将其调试输出馈送到segment输出格式参数.

Use silencedetect audio filter and feed its debugging output to segment output format parameter.

这是现成的脚本:

#!/bin/bash

IN=$1
OUT=$2

true ${SD_PARAMS:="-55dB:d=0.3"};
true ${MIN_FRAGMENT_DURATION:="20"};
export MIN_FRAGMENT_DURATION

if [ -z "$OUT" ]; then
    echo "Usage: split_by_silence.sh input_media.mp4 output_template_%03d.mkv"
    echo "Depends on FFmpeg, Bash, Awk, Perl 5. Not tested on Mac or Windows."
    echo ""
    echo "Environment variables (with their current values):"
    echo "    SD_PARAMS=$SD_PARAMS       Parameters for FFmpeg's silencedetect filter: noise tolerance and minimal silence duration"
    echo "    MIN_FRAGMENT_DURATION=$MIN_FRAGMENT_DURATION    Minimal fragment duration"
    exit 1
fi

echo "Determining split points..." >& 2

SPLITS=$(
    ffmpeg -nostats -v repeat+info -i "${IN}" -af silencedetect="${SD_PARAMS}" -vn -sn  -f s16le  -y /dev/null \
    |& grep '\[silencedetect.*silence_start:' \
    | awk '{print $5}' \
    | perl -ne '
        our $prev;
        INIT { $prev = 0.0; }
        chomp;
        if (($_ - $prev) >= $ENV{MIN_FRAGMENT_DURATION}) {
            print "$_,";
            $prev = $_;
        }
    ' \
    | sed 's!,$!!'
)


echo "Splitting points are $SPLITS"

ffmpeg -v warning -i "$IN" -c copy -map 0 -f segment -segment_times "$SPLITS" "$OUT"

您指定输入文件,输出文件模板,静默检测参数和最小片段大小,它将写入多个文件.

You specify input file, output file template, silence detection parametres and minimum fragment size, it writes multiple files.

可能需要调整沉默检测参数:

Silence detection parameters may need to be tuned:

  • SD_PARAMS环境变量包含两个参数:噪声容忍度和最小静音持续时间.默认值为-55dB:d=0.3.
  • -55dB减小至-70dB如果一些微弱的无声声音在不应该发声时触发吐痰.增加到例如-40dB如果由于有噪音而无法在静音时分裂,则表示它不是完全静音.
  • d=0.3是被视为分裂点的最小静音持续时间.如果仅将严重(例如整整3秒)的静默视为真正的,值得分割的静默,请增加该音量.
  • 另一个环境变量MIN_FRAGMENT_DURATION定义每次拆分后静默事件被忽略的时间.设置最小片段持续时间.
  • SD_PARAMS environment variable contains two parameters: noise tolerance level and minimum silence duration. Default value is -55dB:d=0.3.
  • Decrease the -55dB to e.g. -70dB if some faint non-silent sounds trigger spitting when they should not. Increase it to e.g. -40dB if it does not split on silence because of there is some noise in it, making it not completely silent.
  • d=0.3 is a minimum silence duration to be considered as a splitting point. Increase it if only serious (e.g. whole 3 seconds) silence should be considered as real, split-worthy silence.
  • Another environment variable MIN_FRAGMENT_DURATION defines amount of time silence events are ignored after each split. This sets minimum fragment duration.

如果根本没有检测到静音,脚本将失败.

The script would fail if no silence is detected at all.

Github Gist上有一个重构版本,但是它存在问题一位用户.

There is a refactored version on Github Gist, but there was a problem with it for one user.

这篇关于每当音量为零时,如何用ffmpeg分割mp4视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆