Skip to main content

ffmpeg Black Split

PyPI version

Python package

Split a video based on black frames.

This tool uses the blackdetect filter from ffmpeg to determine the periods of black content.

It can cut the video into segments based on these periods, and also output the detected black and content periods as JSON.

Author: Werner Robitza werner.robitza@gmail.com

Contents:

Requirements

  • Python 3.9 or higher
  • FFmpeg:
    • download a static build from their website)
    • put the ffmpeg executable in your $PATH

Installation

Simply run it via uv:

uvx ffmpeg-black-split

Or install via pipx. Or with pip:

pip3 install --user ffmpeg_black_split

Usage

Run:

ffmpeg-black-split <input-file>

This might take a while depending on the length of your input file. It'll then split the video into parts, prefixed by the original filename. The audio and video streams will be copied as-is.

The output will be placed in the current directory, with each file being named <input>_<start>-<end>.mkv.

Note that by default, cutting is not that accurate, as stream-copying is used. If you want to re-encode using x264, you can use the --no-copy flag. (Future versions may have better options for encoding.)

Choose a different output extension by specifying the --output-extension option. The default is mkv.

Pass the --no-split option to disable the actual splitting.

JSON Output

Example to get just the JSON output:

ffmpeg-black-split input.mkv -p -v --no-split 2>/dev/null

Returns:

{
  "black_periods": [
    {
      "start": 0.0,
      "end": 5.0,
      "duration": 5.0
    },
    {
      "start": 10.0,
      "end": 15.0,
      "duration": 5.0
    },
    {
      "start": 20.0,
      "end": 25.0,
      "duration": 5.0
    }
  ],
  "content_periods": [
    {
      "start": 5.0,
      "end": 10.0
    },
    {
      "start": 10.0,
      "end": 20.0
    },
    {
      "start": 20.0,
      "end": null
    }
  ]
}

Extended Usage

See ffmpeg-black-split -h for more:

usage: ffmpeg-black-split [-h] [-d BLACK_MIN_DURATION] [-r PICTURE_BLACK_RATIO_TH] [-t PIXEL_BLACK_TH]
                   [-o OUTPUT_DIRECTORY] [-e OUTPUT_EXTENSION] [--no-split] [--no-copy] [-p] [-v]
                   [--ffmpeg-path FFMPEG_PATH]
                   input

ffmpeg-black-split v0.4.0

positional arguments:
  input                 input file

options:
  -h, --help            show this help message and exit
  -d BLACK_MIN_DURATION, --black-min-duration BLACK_MIN_DURATION
                        Set the minimum detected black duration expressed in seconds. It must be
                        a non-negative floating point number. (default: 2.0)
  -r PICTURE_BLACK_RATIO_TH, --picture-black-ratio-th PICTURE_BLACK_RATIO_TH
                        Set the threshold for considering a picture 'black' (default: 0.98)
  -t PIXEL_BLACK_TH, --pixel-black-th PIXEL_BLACK_TH
                        Set the threshold for considering a pixel 'black' (default: 0.1)
  -o OUTPUT_DIRECTORY, --output-directory OUTPUT_DIRECTORY
                        Set the output directory. Default is the current working directory.
                        (default: None)
  -e OUTPUT_EXTENSION, --output-extension OUTPUT_EXTENSION
                        Set the ffmpeg output extension. Default is 'mkv'. Choose 'mov' for
                        QuickTime-compatible files. (default: mkv)
  --no-split            Don't split the video into segments. (default: False)
  --no-copy             Don't stream-copy, but re-encode the video. This is useful in case of
                        conversion errors when using different output formats. (default: False)
  -p, --progress        Show a progress bar on stderr (default: False)
  -v, --verbose         Print verbose info to stderr, and JSON of black and content periods to
                        stdout (default: False)
  --ffmpeg-path FFMPEG_PATH
                        Path to ffmpeg executable (default: ffmpeg)

API

The program exposes an API that you can use yourself:

from ffmpeg_black_split import FfmpegBlackSplit

ffbs = FfmpegBlackSplit("input.mkv")
ffbs.detect_black_periods()
ffbs.cut_all_periods("/path/to/output/folder")

For more usage please read the docs.

License

ffmpeg_black_split, Copyright (c) 2022-2024 Werner Robitza

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Metadata

Release files for ffmpeg-black-split 0.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ffmpeg-black-split 0.7.1
File Size Uploaded
ffmpeg_black_split-0.7.1.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ffmpeg-black-split 0.7.1
File Interpreter ABI Platform
ffmpeg_black_split-0.7.1-py3-none-any.whl Python 3 none any Details

Total release size: 18.6 kB

Release files / ffmpeg_black_split-0.7.1.tar.gz

Download URL ffmpeg_black_split-0.7.1.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5dcae7a451e8cdbd5f6268d79feb85194f0a9f71ac3a9469651d3ddb161eda6d
BLAKE2b-256 checksum
How to use checksums
8eedce238cb84abf10193e22ba044d499af1f2eb592fa22ca385cf38f3199931
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / ffmpeg_black_split-0.7.1-py3-none-any.whl

Download URL ffmpeg_black_split-0.7.1-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3d2fdd572752d3d59a48524fa2b2d21feb9b05a9688ed096f8cfdb6e38aaed30
BLAKE2b-256 checksum
How to use checksums
43be832372e238bcd64f045817e3d99880fe872ff6fa8ff02c159502a76792a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.7.1 This release

2 release files

0.7.0

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page