Command line utility ffmpeg, dedicated to different transformations of video, is one of the tools which genuinly feel like magic.

I do not work with video on day to day basis, and when I do, there usually is a deadline looming.

What happens when there is a deadline? I realize couple of things at the last minute:

  • Different format of the video is required.
  • Video is too big and my institutional mail will not cope with this size of attachment.
  • I want to get a gif from the video so that there are no issue when presenting.
  • I usually want to do the same processing to multiple videos.

Initially (being a biologist) I try to use FiJi and fail. Then, I go for some online tool. The online tool usually stalls midway upload or processing, or requires fee/registration for the download.

And the clock is ticking (usually to midnight)

Having this versatile tool (which is also free) on my own machine is invaluable.

Easy to use

ffmpeg download link can do a lot, so it is no surprise that the documentation is overwhelming.

Fortunately, chatGPT helps to put together all the flags need for your usecase.

chat-gpt-prompt

Use cases

Convert between formats

This probably cannot be easier using ffmpeg

  • single video:
ffmpeg -i input_video.mp4 output_video.avi
  • multiple videos:
for file in *.mp4;
do ffmpeg -i "${file}" "${file/.mp4/.avi}"
done

Scale video down

  • single video
ffmpeg -i input_video.mp4 -vf "scale=1280:720" output_video.mp4
  • multiple videos
for file in *.mp4;
do ffmpeg -i "${file}" -vf "scale=1280:720" "${file/.mp4/_scaled.mp4}";
done

Extract audio from video

  • single file:
ffmpeg -i input_video.mp4 -q:a 0 -map a output_audio.mp3
  • many files:
for file in *.mp4; 
do ffmpeg -i "${file}" -q:a 0 -map a "${file/.mp4/.mp3}"; 
done

Change audio from mono to stereo

  • single file: ffmpeg -i input_mono.mp3 -ac 2 output_stereo.mp3

  • multiple files:

for file  in *.mp3; 
do ffmpeg -i "${file}" -ac 2 "${file/.mp3/stereo.mp3}"; 
done

Make gifs from video

GIFs work nearly every time in presentation when compared to video, so how to get them with defined speed and resolution?

  • single file:
ffmpeg -i input_video.mp4 -vf "fps=10,scale=320:-1:flags=lanczos" -c:v gif output.gif`
  • multiple files:
for file in *.mp4;
do ffmpeg -i "${file}" -vf "fps=10,scale=320:-1:flags=lanczos" -c:v gif "${file/.mp4/.gif}
done;

Comparison to FiJi

Below is the chatGPT output when asking how to make a GIF from video:

To convert a video to a GIF using Fiji (ImageJ), follow these steps:

### Step 1: Install Fiji (ImageJ)
- Make sure you have Fiji installed on your computer. Fiji is a distribution of ImageJ, which includes many plugins for various image processing tasks.

### Step 2: Open the Video in Fiji
1. **Launch Fiji**.
2. Go to `File > Import > AVI...` (or another video format, depending on your file) to open your video file.
   - If your video format is not supported directly, you may need to convert it to AVI first or install additional plugins.

### Step 3: Process the Video
1. Once the video is loaded, you can process it if needed (e.g., cropping, resizing, adjusting the frame rate):
   - To crop the video, use the `Rectangle` tool to select the area and then go to `Image > Crop`.
   - To resize the video, go to `Image > Scale...` and set the desired dimensions.
   - To adjust the frame rate, go to `Image > Stacks > Tools > Animation Options...` and set the desired frame rate.

### Step 4: Convert to GIF
1. After processing, convert the video to a GIF:
   - Go to `File > Save As > GIF...`.
   - Set the destination folder and filename.
   - Click `OK` to save the GIF.

### Step 5: Adjust GIF Settings (Optional)
- You can adjust settings like color depth or frame delay by accessing the options before saving as a GIF.

### Example Workflow
If you have a video named `sample_video.avi`:
1. Open Fiji and import the video via `File > Import > AVI...`.
2. Process the video as needed (resize, crop, etc.).
3. Convert and save the video as a GIF via `File > Save As > GIF...`.

### Additional Tips:
- **Plugins**: Fiji/ImageJ has various plugins that can help optimize the GIF, such as reducing the file size or adjusting colors.
- **Frame Rate**: Reducing the frame rate can make the GIF smaller and more manageable.

This workflow should help you convert a video to a GIF using Fiji (ImageJ).