Python Ffmpeg
Python Ffmpeg
Overview
python-ffmpeg is a python binding for FFmpeg which provides sync and async APIs.
Install
To install python-ffmpeg, simply use pip:
Examples
Transcoding
Synchronous API
def main():
ffmpeg = (
FFmpeg()
.option("y")
.input("input.mp4")
.output(
"output.mp4",
{"codec:v": "libx264"},
vf="scale=1280:-1",
preset="veryslow",
crf=24,
)
)
ffmpeg.execute()
if __name__ == "__main__":
main()
Asynchronous API
https://python-ffmpeg.readthedocs.io/en/latest/ 1/3
7/8/24, 11:22 PM python-ffmpeg
import asyncio
await ffmpeg.execute()
if __name__ == "__main__":
asyncio.run(main())
Recording
Synchronous API
def main():
ffmpeg = (
FFmpeg()
.option("y")
.input(
"rtsp://username:[email protected]/cam",
rtsp_transport="tcp",
rtsp_flags="prefer_tcp",
)
.output("output.mp4", vcodec="copy")
)
@ffmpeg.on("progress")
def time_to_terminate(progress: Progress):
if progress.frame > 200:
ffmpeg.terminate()
ffmpeg.execute()
https://python-ffmpeg.readthedocs.io/en/latest/ 2/3
7/8/24, 11:22 PM python-ffmpeg
if __name__ == "__main__":
main()
Asynchronous API
import asyncio
@ffmpeg.on("progress")
def time_to_terminate(progress: Progress):
if progress.frame > 200:
ffmpeg.terminate()
await ffmpeg.execute()
if __name__ == "__main__":
asyncio.run(main())
https://python-ffmpeg.readthedocs.io/en/latest/ 3/3