videogrep

repository·master·Indexed 25 days ago

https://github.com/antiboredom/videogrep

A command-line tool and Python module that searches through dialogue in video and audio files to create 'supercuts' (compilations of specific clips) based on text or regular expression queries. It supports subtitle tracks (.srt, .vtt), transcription files (.json), and built-in transcription via Vosk or pocketsphinx. Output formats include .mp4, .mp3, .m3u, and FCPXML for professional editors like Final Cut Pro, Premiere, and Davinci Resolve.

Tokens
3.8K
Snippets
5
Records
31
Agent score
82%

What's inside videogrep

  1. Transcribe Video or Audio with Vosk

    master

    If you do not have subtitle files, you can generate transcriptions using the --transcribe flag. This uses the vosk engine and generates a .json file in the same directory as the input. By default, it uses the Vosk small English model.

    You can specify a custom Vosk model using the --model flag.

  2. Install Videogrep and Vosk

    master

    Videogrep is compatible with Python 3.6 to 3.10. To install the core tool, use pip install videogrep. If you need to transcribe video or audio files (rather than using existing subtitle tracks), you must also install vosk separately.

    pip install videogrep
    pip install vosk
  3. Basic Usage of Videogrep CLI

    master

    The most basic way to use Videogrep is to provide an input file and a search phrase. The search phrase supports regular expressions.

    Important Requirement: Videogrep requires a matching subtitle track (.srt or .vtt) or a transcription file (.json) for the input file. The subtitle file must have the exact same name as the media file, differing only by the extension (e.g., movie.mp4 and movie.srt).

    videogrep --input path/to/video.mp4 --search 'search phrase'
  4. Use Videogrep as a Python Module

    master
    You can import videogrep into your own Python scripts. The videogrep function accepts parameters similar to the CLI script.
  5. Configure Videogrep CLI Options

    master

    Use the following flags to customize your search and output:

    • --input [filename(s)] / -i [filename(s)]: Input video or audio files. Mixing audio and video inputs results in an audio-only output.
    • --output [filename] / -o [filename]: Name of the generated file (default: supercut.mp4). Supported extensions include .mp4, .mp3, .mpv.edl (for mpv previews), .m3u (playlist), and .xml (Final Cut Pro/Premiere/Davinci Resolve).
    • --search [query] / -s [query]: Search term (regular expression). Can be used multiple times to add multiple search terms.
    • --search-type [type] / -st [type]:
      • sentence (default): Clips containing full sentences.
      • fragment: Clips containing exact words/phrases. Requires word-level timestamps (e.g., YouTube .vtt or files generated via --transcribe).
    • --max-clips [num] / -m [num]: Maximum number of clips in the supercut.
    • --demo / -d: Show search results without generating a supercut.
    • --preview / -pr: Preview the supercut in mpv (requires mpv installed).
    • --randomize / -r: Randomize clip order.
    • --padding [seconds] / -p [seconds]: Add padding to the start and end of each clip.
    • --resyncsubs [seconds] / -rs [seconds]: Shift subtitles forwards or backwards by the specified seconds.
    • --export-clips / -ec: Export clips as individual files instead of a single supercut.
    • --export-vtt / -ev: Export the supercut transcript as a .vtt file.
    • --ngrams [num] / -n [num]: Show common words and phrases from the file.
  6. Transcribe a video file with transcribe()

    master

    The transcribe function converts the audio from a video file into a list of timestamped text segments using the Vosk speech recognition engine.

    Behavioral Notes:

    • Caching: If a .json file with the same name as the video exists in the same directory, the function will load and return that cached transcript instead of re-processing the video.
    • Output Format: Returns a List[dict]. Each dictionary contains:
      • content: A string of the transcribed text (capped at MAX_CHARS, which is 36).
      • start: The start timestamp of the segment.
      • end: The end timestamp of the segment.
      • words: A list of individual word objects containing detailed metadata (e.g., word, start, end).
    • Dependencies: Requires vosk and imageio_ffmpeg to be installed. It uses ffmpeg internally to extract audio at a 16000Hz sample rate.
  7. Extract n-grams from video transcripts using `get_ngrams()`

    master

    Generates n-grams from the transcripts of one or more video files.

    Parameters:

    • files: A single file path or a list of file paths.
    • n: The size of the n-gram (default is 1).

    Returns: An iterator of tuples containing the n-gram and its occurrences.

  8. Transcribe audio using `transcribe`

    master

    Uses pocketsphinx_continuous to transcribe the audio from a video file.

    Workflow:

    1. Checks if a .transcript file already exists for the input file; if so, returns its content.
    2. If not, converts the video to a temporary WAV file using convert_to_wav.
    3. Runs pocketsphinx_continuous with the -time yes flag to include timestamps.
    4. Saves the resulting transcription to a .transcript file.
    5. Cleans up the temporary WAV file.

    Requirements:

    • ffmpeg must be installed and available in the system path.
    • pocketsphinx_continuous must be installed and available in the system path.
  9. Convert segments to SRT format with convert_to_srt()

    master
    The convert_to_srt function converts a list of sentence dictionaries into a single SubRip (SRT) formatted string. Note that this function expects the input dictionaries to have a words key containing timing information and a text key for the content.
  10. Clean and adjust clip segments with `remove_overlaps()` and `pad_and_sync()`

    master

    These utility functions allow for fine-tuning the list of segments (timestamps) before exporting.

    • remove_overlaps(segments): Sorts segments by start time and merges any segments that overlap in time.
    • pad_and_sync(segments, padding=0, resync=0):
      • padding: Adds time to both the start and end of each segment.
      • resync: Shifts the entire segment (start and end) by a specified offset.
      • Also handles merging overlapping segments that belong to the same file.

    Input Format: segments should be a List[dict] where each dict contains {'start': float, 'end': float, 'file': str}.

  11. Search for video segments using `search()`

    master

    The search() function finds timestamps in video files based on a query. It supports three search modes:

    • sentence: Matches the query against the full content of a transcript line using regular expressions.
    • fragment: Matches a sequence of words (a phrase) by looking for word-level timestamps. This requires the transcript to have word-level data.
    • mash: Finds random occurrences of specific words from the query.

    Returns a list of dictionaries in the format: [{'file': str, 'start': float, 'end': float, 'content': str}].

    Parameters:

    • files: A single file path or a list of file paths.
    • query: A single regex string or a list of queries.
    • search_type: One of "sentence", "fragment", or "mash".
    • prefer: (Optional) Preferred transcript file type (vtt, srt, or json).