The Basics of FFmpeg: Command-Line, JavaScript, and Python Video Editing

Nkugwa Mark William
4 min readJul 25, 2023

--

FFmpeg is a powerful multimedia processing tool that can handle audio, video, and other types of multimedia files and streams. It’s renowned for its capabilities to convert and process media files, but it’s more than just a converter. In this article, we’ll walk you through the basics of FFmpeg, then dive deep into editing videos with it using the command-line, JavaScript, and Python.

1. Basics of FFmpeg

FFmpeg is a complete, cross-platform solution that integrates a vast number of libraries and drivers for processing video, audio, and metadata. It consists of:

  • ffmpeg: the command-line tool
  • libavcodec: a library containing all FFmpeg codecs
  • libavformat: a library containing demuxers and muxers for audio/video container formats
  • and many others: such as libavutil, libswscale, etc.

Installation:

  • On macOS: brew install ffmpeg
  • On Ubuntu: sudo apt-get install ffmpeg
  • On Windows: Download builds from Zeranoe’s FFmpeg Builds

2. Editing Videos with FFmpeg from CMD

Trimming a video:

ffmpeg -i input.mp4 -ss 00:01:00 -to 00:02:00 -c:v copy -c:a copy output.mp4

This trims the video from 1 minute to 2 minutes.

Changing resolution:

ffmpeg -i input.mp4 -vf "scale=1280:720" output.mp4

This resizes the video to 1280x720 resolution.

3. Editing Videos with FFmpeg in JavaScript

To work with FFmpeg in JavaScript, you can use the ffmpeg.js library.

Example:

const ffmpeg = require('ffmpeg.js');

let fs = require('fs');
let video = fs.readFileSync('input.mp4');
let result = ffmpeg.run('-i input.mp4 -vf "scale=1280:720" output.mp4');
let output = result[0].data;
fs.writeFileSync('output.mp4', output);

4. Editing Videos with FFmpeg in Python

For Python, you can use the imageio-ffmpeg package, which is a wrapper around FFmpeg.

Installation:

pip install imageio[ffmpeg]

Example:

import imageio
from imageio_ffmpeg import ffprobe

input_path = "input.mp4"
output_path = "output.mp4"

# Using ffprobe to retrieve video metadata
meta_data = ffprobe(input_path)

# Resize the video using FFmpeg via imageio
reader = imageio.get_reader(input_path)
fps = meta_data['video']['@r_frame_rate']

writer = imageio.get_writer(output_path, fps=fps)
for im in reader:
writer.append_data(im[:, :, :])
writer.close()

5. Video Editing Templates

If you’re looking to create a template for video editing, it helps to define the operations (e.g., trimming, resizing, overlaying) you’d want to perform frequently. For example:

  • CMD Template:
ffmpeg -i {input} -vf "scale={width}:{height}" -ss {start_time} -to {end_time} {output}

The Basics of FFmpeg: Command-Line, JavaScript, and Python Video Editing

FFmpeg is a powerful multimedia processing tool that can handle audio, video, and other types of multimedia files and streams. It’s renowned for its capabilities to convert and process media files, but it’s more than just a converter. In this article, we’ll walk you through the basics of FFmpeg, then dive deep into editing videos with it using the command-line, JavaScript, and Python.

1. Basics of FFmpeg

FFmpeg is a complete, cross-platform solution that integrates a vast number of libraries and drivers for processing video, audio, and metadata. It consists of:

  • ffmpeg: the command-line tool
  • libavcodec: a library containing all FFmpeg codecs
  • libavformat: a library containing demuxers and muxers for audio/video container formats
  • and many others: such as libavutil, libswscale, etc.

Installation:

  • On macOS: brew install ffmpeg
  • On Ubuntu: sudo apt-get install ffmpeg
  • On Windows: Download builds from Zeranoe’s FFmpeg Builds

2. Editing Videos with FFmpeg from CMD

Trimming a video:

ffmpeg -i input.mp4 -ss 00:01:00 -to 00:02:00 -c:v copy -c:a copy output.mp4

This trims the video from 1 minute to 2 minutes.

Changing resolution:

ffmpeg -i input.mp4 -vf "scale=1280:720" output.mp4

This resizes the video to 1280x720 resolution.

3. Editing Videos with FFmpeg in JavaScript

To work with FFmpeg in JavaScript, you can use the ffmpeg.js library.

Example:

javascriptCopy code
const ffmpeg = require('ffmpeg.js');
let fs = require('fs');
let video = fs.readFileSync('input.mp4');
let result = ffmpeg.run('-i input.mp4 -vf "scale=1280:720" output.mp4');
let output = result[0].data;
fs.writeFileSync('output.mp4', output);

4. Editing Videos with FFmpeg in Python

For Python, you can use the imageio-ffmpeg package, which is a wrapper around FFmpeg.

Installation:

pip install imageio[ffmpeg]

Example:

import imageio
from imageio_ffmpeg import ffprobe
input_path = "input.mp4"
output_path = "output.mp4"
# Using ffprobe to retrieve video metadata
meta_data = ffprobe(input_path)
# Resize the video using FFmpeg via imageio
reader = imageio.get_reader(input_path)
fps = meta_data['video']['@r_frame_rate']
writer = imageio.get_writer(output_path, fps=fps)
for im in reader:
writer.append_data(im[:, :, :])
writer.close()

5. Video Editing Templates

If you’re looking to create a template for video editing, it helps to define the operations (e.g., trimming, resizing, overlaying) you’d want to perform frequently. For example:

CMD Template:

ffmpeg -i {input} -vf "scale={width}:{height}" -ss {start_time} -to {end_time} {outp

JavaScript Template:

let result = ffmpeg.run(`-i ${input} -vf "scale=${width}:${height}" -ss ${start_time} -to ${end_time} ${output}`);

Python Template:

import imageio
from imageio_ffmpeg import ffprobe

def process_video(input, output, width, height, start_time, end_time):
meta_data = ffprobe(input)
reader = imageio.get_reader(input)
fps = meta_data['video']['@r_frame_rate']
writer = imageio.get_writer(output, fps=fps)
for im in reader:
writer.append_data(im[:, :, :])
writer.close()

In conclusion, FFmpeg is an incredibly versatile tool for media processing. With its cross-language support, it can cater to a wide variety of use cases and platforms. Whether you’re working directly from the command line or incorporating it into a larger app using JavaScript or Python, FFmpeg has got you covered.

--

--

Nkugwa Mark William
Nkugwa Mark William

Written by Nkugwa Mark William

Nkugwa Mark William is a Chemical and Process engineer , entrepreneur, software engineer and a technologists with Apps on google play store and e commerce sites

No responses yet