library
ffmpeg
The ffmpeg module (Python wrapper around the powerful FFmpeg command-line tool) is used to manipulate audio/video files and streams. Here's a breakdown of all common use cases of the ffmpeg module (such as ffmpeg-python):
🔧 1. Transcoding (Convert Format)
import ffmpeg
ffmpeg.input('input.mp4').output('output.avi').run()✅ Convert between formats like .mp4, .avi, .mkv, .mp3, .wav, etc.
🗜️ 2. Compress Video/Audio
ffmpeg.input('input.mp4').output('output.mp4', video_bitrate='800k', audio_bitrate='128k').run()✅ Reduce file size by adjusting bitrate.
📐 3. Resize / Scale Video
ffmpeg.input('input.mp4').output('output.mp4', vf='scale=1280:720').run()✅ Resize video resolution.
✂️ 4. Trim / Cut Audio or Video
ffmpeg.input('input.mp4', ss=10, t=5).output('clip.mp4').run()✅ Trim 5 seconds starting at 10 seconds.
🔈 5. Extract Audio from Video
ffmpeg.input('input.mp4').output('output.mp3').run()✅ Save only the audio track.
🎞️ 6. Extract Frames from Video
ffmpeg.input('input.mp4').output('frame_%03d.jpg', vf='fps=1').run()✅ Extract 1 frame per second.
🧵 7. Merge Videos
ffmpeg.input('input1.mp4').output('input2.mp4').concat(v=1, a=1).output('merged.mp4').run()✅ Merge two video files (both must have same codec and resolution).
🔇 8. Remove Audio from Video
ffmpeg.input('input.mp4').output('no_audio.mp4', an=None).run()✅ Strip out audio track.
🖼️ 9. Convert Images to Video
ffmpeg.input('images/frame_%03d.jpg', framerate=24).output('slideshow.mp4').run()✅ Create video from image sequence.
🔤 10. Burn Subtitles into Video
ffmpeg.input('input.mp4').output('output.mp4', vf='subtitles=subtitle.srt').run()✅ Add hardcoded subtitles.
🔁 11. Loop Video or Audio
ffmpeg.input('input.mp4', stream_loop=3).output('looped.mp4').run()✅ Repeat media multiple times.
🔉 12. Adjust Volume
ffmpeg.input('input.mp3').output('output.mp3', af='volume=1.5').run()✅ Increase or decrease volume.
👨🎤 13. Add Background Audio to Video
video = ffmpeg.input('video.mp4')
audio = ffmpeg.input('music.mp3')
ffmpeg.output(video, audio, 'output.mp4', vcodec='copy', acodec='aac').run()✅ Overlay music or narration.
🏷️ 14. Read Metadata
import ffmpeg
probe = ffmpeg.probe('input.mp4')
print(probe['format']['duration'])✅ Get video duration, bitrate, etc.
📡 15. Stream to Server (RTMP, HLS)
ffmpeg.input('input.mp4').output('rtmp://server/app/stream_key').run()✅ Use for live streaming to YouTube, Twitch, etc.
🔤 16. Add Watermark
ffmpeg.input('input.mp4').output('output.mp4', vf="movie=logo.png [watermark]; [in][watermark] overlay=10:10 [out]").run()✅ Add image watermark on video.
📁 Common File Formats Handled
Video:
.mp4,.avi,.mkv,.mov,.flvAudio:
.mp3,.wav,.aac,.ogg,.flacImage sequences:
.jpg,.png,.bmp
Would you like examples with CLI commands, more advanced filtering, or automation scripts?
Last updated