Skip to main content

Run ffmpeg and ffprobe commands with nicely parsed output.

Project description

FFmpeg output parser

Overview

Do you already know the ffmpeg command line, and don't want to relearn some syntax of a pythonic ffmpeg wrapper? This is the package for you. Just put in an ffmpeg or ffprobe command and this package structures the output while it's processing.

Usage

The code below converts a video, and prints the percentage completion while it's working. This example includes optional error handling, output shown below.

from parsed_ffmpeg import run_ffmpeg, FfmpegError


async def process_video():
    try:
        await run_ffmpeg(
            f"ffmpeg -i input.mp4 -c:v libx264 output.mp4",
            on_status=lambda status: print(f"We're: {status.completion * 100:.1f}% there!"),
            overwrite_output=True
        )
        print("Done!")
    except FfmpegError as e:
        print(f"ffmpeg failed with error: {e}")

Example output: custom status logging

We're: 8.2% there!
We're: 45.5% there!
We're: 100.0% there!
Done!

Error example

ffmpeg failed with error: 

	User command:
		ffmpeg -i input.mp4 -c:v libx264 output.mp4
	Full command:
		ffmpeg -i input.mp4 -c:v libx264 output.mp4 -y -progress pipe:1
	Working directory:
		C:\Users\rutenl\PycharmProjects\parsed_ffmpeg

[in#0 @ 00000208d2d4e1c0] Error opening input: No such file or directory
Error opening input file input.mp4.
Error opening input files: No such file or directory

Example: Ffprobe

Ffprobe output is also supported, use it like this:

input_video = "input.mp4"
result = await run_ffprobe(f"ffprobe {input_video}")
print(str(result))

The ffprobe result includes this info:

{
  "bitrate_kbs": 3293,
  "duration_ms": 6840,
  "start_time": 0.0,
  "streams": [
    {
      "bitrate_kbs": 3152,
      "codec": "h264",
      "details": "yuv420p(tv, smpte170m, progressive), 1280x720 [SAR 1:1 DAR 16:9], 3152 kb/s, 60 fps, 60 tbr, 15360 tbn (default)",
      "type": "video",
      "resolution_w": 1280,
      "resolution_h": 720,
      "fps": 60
    },
    {
      "bitrate_kbs": 128,
      "codec": "aac",
      "details": "48000 Hz, stereo, fltp, 128 kb/s (default)",
      "type": "audio",
      "sample_rate": 48000,
      "channels": ["stereo", "fltp"]
    }
  ]
}

Example: run with tqdm to get a progressbar

If you install the tqdm extra dependency (pip install parsed-ffmpeg[tqdm]), you can do the following:

input_video = Path(__file__).parent.parent / "tests/assets/input.mp4"
await run_ffmpeg(
    f"ffmpeg -i {input_video} -vf scale=-1:1440 -c:v libx264 output.mp4",
    print_progress_bar=True,
    progress_bar_description=input_video.name,
    overwrite_output=True,
)

It'll give output like this:

input.mp4:  73%|███████▎  | 4466/6084 [00:04<00:00, 1620.10ms/s]

Installation

Remember that this package does not come with an ffmpeg binary, you have to have it in path or point to it in your command.

pip install parsed-ffmpeg

API

run_ffmpeg

async def run_ffmpeg(
    command: list[str] | str,
    on_status: Callable[[FfmpegStatus], None] | None = None,
    on_stdout: Callable[[str], None] | None = None,
    on_stderr: Callable[[str], None] | None = None,
    on_error: Callable[[list[str]], None] | None = None,
    on_warning: Callable[[str], None] | None = None,
    overwrite_output: bool = False,
    raise_on_error: bool = True,
    print_progress_bar: bool = False,
    progress_bar_description: str | None = None,
) -> str:
    ...

StatusUpdate

class StatusUpdate:
    frame: int | None
    fps: float | None
    bitrate: str | None
    total_size: int | None
    out_time_ms: float | None
    dup_frames: int | None
    drop_frames: int | None
    speed: float | None
    progress: str | None
    duration_ms: int | None
    completion: float | None

run_ffprobe

async def run_ffprobe(
    command: list[str | Path] | str,
    on_error: Callable[[list[str]], None] | None = None,
    on_warning: Callable[[str], None] | None = None,
    raise_on_error: bool = True,
) -> FfprobeResult:
    ...

ffprobe types

class AudioStream(BaseStream):
    bitrate_kbs: int
    codec: str
    details: str
    type: StreamType #(video or audio)
    sample_rate: int
    channels: list[str]

class VideoStream(BaseStream):
    bitrate_kbs: int
    codec: str
    details: str
    type: StreamType #(video or audio)
    resolution_w: int
    resolution_h: int
    fps: int

class FfprobeResult:
    bitrate_kbs: int
    duration_ms: int
    start_time: float
    streams: list[BaseStream]

Changing ffmpeg install location

Just replace the first part of your command (ffmpeg) with the path to ffmpeg. Example:

await run_ffmpeg("C:/apps/ffmpeg.exe -i input.mp4 -c:v libx264 output.mp4 -y")

License

MIT

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

parsed_ffmpeg-0.3.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

parsed_ffmpeg-0.3.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file parsed_ffmpeg-0.3.1.tar.gz.

File metadata

  • Download URL: parsed_ffmpeg-0.3.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for parsed_ffmpeg-0.3.1.tar.gz
Algorithm Hash digest
SHA256 1c4c07f0e55e0a61d9b455d1f1c5e72beb92a53f5670505cb9e557cc6e8b605e
MD5 c9bc8943e9182c9e3da81c3b7b8ccc3b
BLAKE2b-256 9538d456c367558669d20a3f1fd15a6150da8a65aaf54803f57a86fbcc5261f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for parsed_ffmpeg-0.3.1.tar.gz:

Publisher: publish-to-pypi.yml on RuurdBijlsma/parsed_ffmpeg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parsed_ffmpeg-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: parsed_ffmpeg-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for parsed_ffmpeg-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a638a31e607bf9e9ca9416ad0e04b0a555269d4a2fc7bd2135b3b7ed9be6f42a
MD5 1fb3b4c8f4957b7945cccc66b1d46f89
BLAKE2b-256 7279be7106c85e79e1e4268a1ee531f71883f9e8989d9bb6ef6ea6f66c2629c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for parsed_ffmpeg-0.3.1-py3-none-any.whl:

Publisher: publish-to-pypi.yml on RuurdBijlsma/parsed_ffmpeg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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