DEV Community

Cover image for ๐Ÿ› ๏ธ Edit videos in terminal
Anthony
Anthony

Posted on

๐Ÿ› ๏ธ Edit videos in terminal

I have been making some videos using AI and I couldn't resist editing it with an awesome CLI tool ffmpeg.
Here are some of my favorite ffmpeg commands for quick video editing.

  • Overlay audio over a video, lowering the original volume by 80%
#!/bin/bash
video="$1"
audio="$2"
ffmpeg \
    -i "$video" \
    -i "$audio" \
    -filter_complex \
    "[1:a]apad[A];[0:a]volume=0.20,[A]amerge[out]" \
    -c:v copy -map 0:v -map [out] -y \
    "processed_${video:0:10}.mp4"
Enter fullscreen mode Exit fullscreen mode
  • Make a video from a single image
#!/bin/bash
image="$1"
audio="$2"
length="$3" #Hh:Mm:Ss
ffmpeg \
    -r 0.01 -loop 1 -i "$image" \
    -i "$audio" -c:v libx264 \
    -tune stillimage -preset ultrafast \
    -ss 00:00:00 -t "$length" \
    -c:a aac -b:a 96k -pix_fmt yuv420p \
    -shortest out.mp4 -y
Enter fullscreen mode Exit fullscreen mode
  • Concat multiple videos (listed in a file) into one
#!/bin/bash
ffmpeg \
  -f concat -safe 0 \
  -i mylist.txt -c copy output.mp4
Enter fullscreen mode Exit fullscreen mode
$ cat mylist.txt
 file '/path/to/file1'
 file '/path/to/file2'
 file '/path/to/file3'
Enter fullscreen mode Exit fullscreen mode
  • Split a video given a timestamp
#!/bin/bash
video="$1"
timestamp="$2" # Hh:Mm:Ss

ffmpeg -i "$video" -to "$timestamp" -c copy first.mp4
ffmpeg -i "$video" -ss "$timestamp" -c copy second.mp4
Enter fullscreen mode Exit fullscreen mode
  • Trim beginning of the video (Removing ads, intros)
#!/bin/bash
video="$1"
time="$2"
ffmpeg \
    -i "$video" -vcodec copy \
    -c:a copy -map 0 -ss "$time" \ 
        "trimmed_${video:0:10}.mp4"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)