Scale Video and Remove Audio from Videos with ffmpeg-python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import ffmpeg

def has_audio(path):
    probe = ffmpeg.probe(path)
    return next(
        (stream for stream in probe["streams"] if stream["codec_type"] == "audio"), None
    )

path = "" # path to video
target_width = 300

vid = ffmpeg.input(path)

scaled = vid.filter("scale", target_width, -2)

if has_audio(path):
    trimmed = vid.audio.filter("atrim", start=0, end=0)
    "Put the scaled video and trimmed audio track back together"
    joined = ffmpeg.concat(scaled, trimmed, v=1, a=1).node
    scaled = joined.stream()

2023-01-15