Skip to content

Instantly share code, notes, and snippets.

@JySzE
Last active January 11, 2025 23:01
Show Gist options
  • Save JySzE/bb1736e1270b38aff1693fd6d1a92338 to your computer and use it in GitHub Desktop.
Save JySzE/bb1736e1270b38aff1693fd6d1a92338 to your computer and use it in GitHub Desktop.

Guide to Generating Keyframes with Batch

This guide explains how to use the provided batch script to generate keyframes from video files.

The script is designed for use with AviSynth and requires specific tools and configurations.


Requirements

  1. AviSynth+ (64-bit version)
    Download and install AviSynth+ from its official GitHub repository.

  2. Plugins (64-bit versions)
    Place the following files in your AviSynth+ plugins folder (C:\Program Files (x86)\AviSynth+\plugins64+):

  3. AVSMeter64
    Add AVSMeter64 to your system's PATH environment variable. You can download it from its official repository or source.

  4. Input Video Requirements

    • Format: YUV420 8-bit
    • No GPU Acceleration: Ensure the video streams are not encoded using NVENC or any GPU-accelerated encoder, as these can have broken keyframes.

Batch Script Overview

The batch script processes all .mkv files in the directory and creates an AviSynth script for each file. It then runs AVSMeter64 to generate keyframes.


Steps to Use the Script

  1. Prepare Your Environment

    • Place all .mkv files you want to process in a single folder.
    • Ensure the videos meet the input requirements (YUV420 8-bit, no GPU acceleration).
  2. Save the Batch Script

    • Copy the script into a .bat file. For example, name it generate_keyframes.bat.
  3. Run the Script

    • Double-click the .bat file or run it from the command line within the folder containing your .mkv files.
  4. Script Execution

    • The script creates an AviSynth script (.avs) for each .mkv file in the folder.
    • It uses the AviSynth plugins (FFMS2 and SCXvid) to process the video and output keyframes.

Script Breakdown

@echo off
for %%f in (*.mkv) do (
    (
        echo FFvideosource^("%%f"^)
        echo Assert^(last.IsYV12^(^), "The clip is not in YUV420P format (YV12)"^)
        echo SCXvid^(log="%%~nf_keyframes.log"^)
    ) > "%%~nf_keyframes.avs"

    avsmeter64 -o "%%~nf_keyframes.avs"
)
pause
  1. Iterating Through Files
    The for loop processes each .mkv file in the current folder.

  2. Creating AviSynth Scripts

    • Generates an .avs file named after the video file.
    • Uses FFvideosource to load the video.
    • Assert ensures that the input clip is YV12.
    • Invokes SCXvid to generate a keyframes log file.
  3. Running AVSMeter64

    • Executes the .avs script using AVSMeter64.
    • The keyframe logs are saved as filename_keyframes.log.

Example Workflow

  1. Place video1.mkv and video2.mkv in a folder.
  2. Run the batch script.
  3. Output:
    • video1_keyframes.avs and video1_keyframes.log
    • video2_keyframes.avs and video2_keyframes.log

Troubleshooting

  • "File not found" errors:
    Check if AVSMeter64 is in the PATH and all required plugins are correctly installed.

  • Keyframe logs are empty or incorrect:
    Ensure the input videos meet the requirements (YUV420 8-bit, no GPU acceleration).

  • Plugins not loading:
    Verify that the SCXvid.dll and ffms2.dll files are in the plugins64+ folder for AviSynth+.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment