Skip to content

Instantly share code, notes, and snippets.

@JySzE
Created January 2, 2025 01:18
Show Gist options
  • Save JySzE/adcc9858ac7a70da7283c43941cad376 to your computer and use it in GitHub Desktop.
Save JySzE/adcc9858ac7a70da7283c43941cad376 to your computer and use it in GitHub Desktop.

Parsing Video Metadata with Batch

Note: This script can be easily adapted or reduced to extract different metadata entries by modifying the findstr lines to match the desired fields.

The metadata is saved in a text file named output.txt.


Prerequisites

  1. FFmpeg: Ensure you have FFmpeg installed on your system.

    • FFmpeg includes ffprobe, which is required for this script.
    • Add FFmpeg to your system's PATH to use it from the command line.
    • Download FFmpeg
  2. File Setup:

    • Place your video files in the folder specified in the script.
    • Update the folder variable in the script with the correct path to your video files.
    • Update the file_ext variable in the script with the correct extension for your video files.

Script

@echo off
setlocal enabledelayedexpansion

set "file_ext=<EXT>"
set "folder=<SOURCEDIR>"
set "output_file=output.txt"

echo. > "%output_file%"

for %%A in ("%folder%\*.%file_ext%") do (
    set "filename_written=false"

    for /f "usebackq tokens=* delims=" %%B in (`ffprobe -v quiet -print_format json -show_format -show_streams "%%A"`) do (
        set "line=%%B"

        if !filename_written! == false (
            echo Filename: %%~nxA >> "%output_file%"
            set "filename_written=true"
        )

        rem Check for color_transfer
        echo !line! | findstr /C:"\"color_transfer\":" > nul
        if !errorlevel! == 0 (
            echo !line! >> "%output_file%"
        )

        rem Check for color_primaries
        echo !line! | findstr /C:"\"color_primaries\":" > nul
        if !errorlevel! == 0 (
            echo !line! >> "%output_file%"
        )

        rem Check for color_space
        echo !line! | findstr /C:"\"color_space\":" > nul
        if !errorlevel! == 0 (
            echo !line! >> "%output_file%"
        )
    )
    echo. >> "%output_file%"
)

pause

Script Explanation

  1. Batch Initialization:

    • setlocal enabledelayedexpansion: Enables delayed variable expansion to process variables dynamically within loops.
  2. Variables:

    • file_ext: The extension of the video files to be processed (e.g., mov, mp4, avi).
    • folder: The path to the folder containing video files.
    • output_file: The name of the output text file where results are saved.
  3. Main Logic:

    • The outer for loop iterates over each video file matching the specified extension in the folder.
    • The inner for /f loop reads metadata output from ffprobe for the current file.
  4. Metadata Filtering:

    • The script checks for lines containing color_transfer, color_primaries, and color_space using findstr.
    • If a match is found, it appends the filename and the matching line to the output file.
  5. Output:

    • Metadata is written to output.txt, located in the same directory as the script.
    • Each video file's metadata is separated by a blank line for clarity.
  6. Pause:

    • The script pauses at the end, allowing you to review any output in the command prompt.

How to Use

  1. Edit the Script:

    • Open the script in a text editor (e.g., Notepad).

    • Update the folder variable with the full path to the folder containing your video files.

      Example:

      set "folder=C:\Users\<YOURUSERNAME>\Videos"
    • Update the file_ext variable with the extension of your video files.

      Example:

      set "file_ext=mp4"
  2. Save the Script:

    • Save the file with a .bat extension, e.g., extract_metadata.bat.
  3. Run the Script:

    • Double-click the .bat file to execute it.
    • Alternatively, you can run it from the command prompt:
      path\to\extract_metadata.bat
  4. Check the Output:

    • After the script completes, open output.txt in a text editor to review the extracted metadata.

Customizations

  • Change the Output File: Modify the output_file variable to use a different file name or location.

    Example:

    set "output_file=C:\Users\<YOURUSERNAME>\Desktop\metadata_output.txt"
  • Filter Additional Metadata: Add more findstr conditions for other metadata fields.

    Example:

         rem Check for Codec
         echo !line! | findstr /C:"\"codec_tag_string\":" > nul
         if !errorlevel! == 0 (
             echo !line! >> "%output_file%"
         )

Troubleshooting

  • FFmpeg Not Recognized: Ensure FFmpeg is installed and added to your system's PATH.

  • Empty Output File: Verify that:

    • Files with the specified extension exist in the folder.
    • ffprobe is functioning properly by testing it manually:
      ffprobe -v quiet -print_format json -show_format -show_streams path\to\video.ext
  • Script Errors: Check for syntax errors in the script and ensure the paths are correct.

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