A python binding for FFmpeg which provides sync and async APIs
See documentation for more details.
To install python-ffmpeg, simply use pip:
$ pip install python-ffmpeg
You can find more examples in the documentation.
from ffmpeg import FFmpeg
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()
import asyncio
from ffmpeg.asyncio import FFmpeg
async def main():
ffmpeg = (
FFmpeg()
.option("y")
.input("input.mp4")
.output(
"output.mp4",
{"codec:v": "libx264"},
vf="scale=1280:-1",
preset="veryslow",
crf=24,
)
)
await ffmpeg.execute()
if __name__ == "__main__":
asyncio.run(main())
from ffmpeg import FFmpeg, Progress
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()
if __name__ == "__main__":
main()
import asyncio
from ffmpeg import Progress
from ffmpeg.asyncio import FFmpeg
async 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()
await ffmpeg.execute()
if __name__ == "__main__":
asyncio.run(main())