Skip to main content

Calculate the magnitude spectrum of a video sequence, via Fast Fourier Transform

Project description

video-fft

PyPI version

Python package

Calculate the magnitude spectrum of a video sequence, via Fast Fourier Transform, to detect the actual resolution.

Contents:

What this does

The package calculates the magnitude spectrum of each frame's luminance data.

This allows, for example, identifying upscaled parts of a video, such as when someone upscaled HD content to UHD. By analyzing the spectrum, you can see that the actual high frequencies that would be expected for "real" UHD content are not present, or not as present as you'd normally see.

The above example shows three sources (each source is one row), with the left image corresponding to the HD version of the source, upscaled to UHD, and the right image being the actual UHD source. The grey "boxes" in the left column tell you that the actual content is merely HD.

The package also outputs the azimuthally averaged 1D power spectrum, from which the high frequencies are extracted and summed up to give one "score" per frame.

Requirements

  • Python 3.8 or higher
  • FFmpeg libraries (to run pyav)

Under Ubuntu, to get ffmpeg libraries:

sudo apt install libavformat-dev libavcodec-dev libavdevice-dev libavutil-dev libavfilter-dev libswscale-dev libavresample-dev

Under macOS, it is recommended to install ffmpeg via Homebrew:

brew install ffmpeg

Installation

pip3 install video-fft

Usage

To run it, simply call video-fft with the path to your video file:

video-fft /path/to/video.mp4

This will output a progress bar for each frame, and finally output the statistics as JSON.

You can enable output of magnitude spectrum images via three options:

  • -r/--first-frame: Only output the first frame of the video
  • -a/--all-frames: Output all frames
  • -m/--mean: Output an averaged magnitude spectrum for all frames

The images will be saved next to the input file, unless you choose a different output path (via -o/--output).

For more info, see video-fft -h:

usage: video-fft [-h] [-o OUTPUT] [-n NUM_FRAMES] [-of {json,csv}] [-r] [-a] [-m] [-s SCALE] [-q] input

positional arguments:
  input                 Input video file

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Output path for the images, default: same as input video file (default: None)
  -n NUM_FRAMES, --num-frames NUM_FRAMES
                        Number of frames to calculate (default: None)
  -of {json,csv}, --output-format {json,csv}
                        Select output format (default: json)
  -r, --first-frame     Render image for radial profile of the first frame (default: False)
  -a, --all-frames      Render image for radial profile of all frames (default: False)
  -m, --mean            Render image for mean/average radial profile of the entire sequence (default: False)
  -s SCALE, --scale SCALE
                        Image scaling, adjust to increase/decrease rendered image size (default: 1)
  -q, --quiet           Do not show progress bar (default: False)

API

The program exposes an API that you can use yourself:

from video_fft import VideoFftCalculator

vid_fft = VideoFftCalculator(
    "input.mp4",
    output="/path/to/output"
)
vid_fft.calc_fft()
print(vid_fft.get_formatted_stats())

For more usage please read the docs.

Limitations

The absolute value of the FFT is not meaningful on its own and cannot be linked to the resolution of the input video. The plots also do not indicate any kind of resolution.

Acknowledgements

This tool is based on initial work from Julian Zebelein and Steve Göring (TU Ilmenau).

The concept is based on the following paper: Katsavounidis, Ioannis, Anne Aaron, and David Ronca. "Native resolution detection of video sequences." SMPTE 2015 Annual Technical Conference And Exhibition. SMPTE, 2015.

License

The MIT License (MIT)

Copyright (c) 2021-2023 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.

Changelog

v0.3.0 (2023-01-10)

  • Various improvements.

    add API, types, github workflows

v0.2.0 (2022-08-02)

  • Fix version variable name.

  • Update python requirements.

  • Fix pip install command.

  • Fix badge link.

v0.1.0 (2021-05-08)

  • Initial commit.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

video_fft-0.3.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

video_fft-0.3.0-py2.py3-none-any.whl (9.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file video_fft-0.3.0.tar.gz.

File metadata

  • Download URL: video_fft-0.3.0.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for video_fft-0.3.0.tar.gz
Algorithm Hash digest
SHA256 3de21a66bb61524f1dee5a918eee2207ed430058948c3eb3a1997931c6fb579b
MD5 f697a2ba21be138d4eb1ef9aedabe784
BLAKE2b-256 e0ccbdf07c798454dc85be10e282fd2e994ab7c6954a0a3d77c12e91f383047b

See more details on using hashes here.

File details

Details for the file video_fft-0.3.0-py2.py3-none-any.whl.

File metadata

  • Download URL: video_fft-0.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for video_fft-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 df2751fc101f8123425c114d6e1c3a18ca73e5929cb0f36319812945eb5f942e
MD5 19aa9fe235de6f8df3714dd84f68ff72
BLAKE2b-256 e4813167c08fa86b73a97b1ff29b2dadd1aab241f84d89e520e0d363b0d12d7d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page