Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:869413421 Ai moive studio FFmpeg Utils

From Leeroopedia
Knowledge Sources
Domains Video, Multimedia
Last Updated 2026-10-23 12:00 GMT

Overview

Utility module providing FFmpeg CLI wrappers for video concatenation (crossfade/fast), speed adjustment, BGM mixing, audio duration measurement, and FFmpeg installation checks.

Description

ffmpeg_utils is a collection of functions that construct and execute FFmpeg CLI commands via subprocess. It provides:

  • check_ffmpeg_installed(): Verifies FFmpeg availability with caching
  • concatenate_videos(): Concatenates video segments with crossfade (xfade filter) or fast (concat demuxer) modes, configurable transition type and duration
  • apply_video_speed(): Adjusts video speed by a multiplier using the setpts and atempo filters
  • mix_bgm_with_video(): Mixes background music with video audio at a configurable volume, with optional BGM looping
  • get_audio_duration(): Measures audio/video duration via ffprobe
  • build_sentence_video_command(): Constructs the FFmpeg command for per-sentence video composition
  • run_ffmpeg_command(): Executes an FFmpeg command with timeout and error handling

Usage

Import when you need to perform video processing operations via FFmpeg. Used by both VideoCompositionService and VideoSynthesisService.

Code Reference

Source Location

Signature

def check_ffmpeg_installed() -> bool: ...

def concatenate_videos(
    video_paths: List[Path],
    output_path: Path,
    concat_file: Path,
    mode: str = "crossfade",
    transition_type: str = "fade",
    transition_duration: float = 0.5
) -> bool: ...

def apply_video_speed(input_path: str, output_path: str, speed: float) -> bool: ...

def mix_bgm_with_video(
    video_path: str, bgm_path: str, output_path: str,
    bgm_volume: float = 0.15, loop_bgm: bool = True
) -> bool: ...

def get_audio_duration(file_path: str) -> float: ...

def build_sentence_video_command(
    image_path, audio_path, output_path, subtitle_filter, gen_setting
) -> list: ...

def run_ffmpeg_command(command: list, timeout: int = 300) -> tuple: ...

Import

from src.utils.ffmpeg_utils import (
    concatenate_videos, apply_video_speed, mix_bgm_with_video,
    get_audio_duration, check_ffmpeg_installed,
    build_sentence_video_command, run_ffmpeg_command
)

I/O Contract

Inputs (concatenate_videos)

Name Type Required Description
video_paths List[Path] Yes Ordered video segment paths
output_path Path Yes Final output path
concat_file Path Yes Temp concat list file
mode str No "crossfade" or "fast" (default "crossfade")
transition_type str No Transition effect (default "fade")
transition_duration float No Transition length in seconds (default 0.5)

Outputs

Name Type Description
concatenate_videos bool Success flag; writes output MP4
apply_video_speed bool Success flag
mix_bgm_with_video bool Success flag
get_audio_duration float Duration in seconds

Usage Examples

Concatenating with Crossfade

from src.utils.ffmpeg_utils import concatenate_videos
from pathlib import Path

success = concatenate_videos(
    video_paths=[Path("seg1.mp4"), Path("seg2.mp4"), Path("seg3.mp4")],
    output_path=Path("final.mp4"),
    concat_file=Path("concat.txt"),
    mode="crossfade",
    transition_type="fade",
    transition_duration=0.5,
)

Mixing BGM

from src.utils.ffmpeg_utils import mix_bgm_with_video

success = mix_bgm_with_video(
    video_path="final_video.mp4",
    bgm_path="bgm.mp3",
    output_path="final_with_bgm.mp4",
    bgm_volume=0.15,
    loop_bgm=True,
)

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment