<commit_before>---
layout: post
title: Editing video and applying effects with ffmpeg from the command line
---

I've started reviewing footage I shot this past fall on a roadtrip a friend and I took to West Virginia. My goal is to create a trip diary / 'road movie'. I initially thought I would use [Cinelerra](http://cinelerra.org) for the project, but when I started googling how to apply a slow motion effect to a clip, I realized I was in for a much more tedious experience than I had initially imagined.

Somewhere along my search, I came across [advice from a fellow moviemaker](http://blog.grio.com/2012/01/fast-and-slow-motion-video-with-ffmpeg.html) (thanks, Andrew!) to simply process the video clip in question directly using ffmpeg. At first I thought this would be a good workaround, and I would import the result into my Cinelerra project and move on. But then it occurred to me: why not put together the entire movie with ffmpeg from the terminal, eschewing the GUI experience altogether?

If we're being honest, Cinelerra's GUI is a tad, well...inscrutable. Would learning ffmpeg's command structure and extensive collection of video and audio filters be any more difficult? That's probably debatable, but doing the whole thing in ffmpeg sounded like a fun challenge to me, so here we are.

I'll lay out a couple of tips and tricks I've picked up so far, and as the project progresses, I hope to document my further findings in a later post.

- - - -

<h4>Slow Motion</h4>

`ffmpeg -i [input-file] -vf "setpts=[your multiplier here]*PTS" [output-file]`

For example, passing "setpts=2\*PTS" to the -vf flag would slow the video down by half. Using a multiplier of less than 1 would speed the video up (i.e. "0.5\*PTS" == double speed).

<h4>Slowing Down the Audio</h4>

You might be surprised, like I was after my first attempt, that slowing down the video as shown above does not automatically slow down the audio as well. Running the above command will give you a slowed down video, but with the audio unchanged, and silence after the original audio runs out.

`ffmpeg -i [input-file] -af "asetrate=[desired sample rate]" [output-file]`

If you wanted to slow down the audio by half, and your original clip has a sample rate of 44100 Hz, you would specify a sample rate of 22050.

You may need to resample your result back up to a 'normal' sample rate to get it to play nice with other clips, depending on what you're doing (44100 and 48000 are common):

`ffmpeg -i [input-file] -af "aresample=[desired sample rate]" [output-file]`

<h4>Chopping a Clip</h4>

[This StackOverflow answer](http://superuser.com/a/141343) helped me figure this one out:

`ffmpeg -i [input-file] -ss [start time] -c copy -t [length of time] [output-file]`

Times can simply be specified in seconds, or in HH:MM:SS.[optional decimal fraction of a second] format.

- - - -

These are just a couple of initial findings - I hope to make another post or two about the subject in the future as I get deeper into the project. May this inspire you to become a command line auteur!<commit_msg>add clarification about using -vf and -af flags together<commit_after>---
layout: post
title: Editing video and applying effects with ffmpeg from the command line
---

I've started reviewing footage I shot this past fall on a roadtrip a friend and I took to West Virginia. My goal is to create a trip diary / 'road movie'. I initially thought I would use [Cinelerra](http://cinelerra.org) for the project, but when I started googling how to apply a slow motion effect to a clip, I realized I was in for a much more tedious experience than I had initially imagined.

Somewhere along my search, I came across [advice from a fellow moviemaker](http://blog.grio.com/2012/01/fast-and-slow-motion-video-with-ffmpeg.html) (thanks, Andrew!) to simply process the video clip in question directly using ffmpeg. At first I thought this would be a good workaround, and I would import the result into my Cinelerra project and move on. But then it occurred to me: why not put together the entire movie with ffmpeg from the terminal, eschewing the GUI experience altogether?

If we're being honest, Cinelerra's GUI is a tad, well...inscrutable. Would learning ffmpeg's command structure and extensive collection of video and audio filters be any more difficult? That's probably debatable, but doing the whole thing in ffmpeg sounded like a fun challenge to me, so here we are.

I'll lay out a couple of tips and tricks I've picked up so far, and as the project progresses, I hope to document my further findings in a later post.

- - - -

<h4>Slow Motion</h4>

`ffmpeg -i [input-file] -vf "setpts=[your multiplier here]*PTS" [output-file]`

For example, passing "setpts=2\*PTS" to the -vf flag would slow the video down by half. Using a multiplier of less than 1 would speed the video up (i.e. "0.5\*PTS" == double speed).

<h4>Slowing Down the Audio</h4>

You might be surprised, like I was after my first attempt, that slowing down the video as shown above does not automatically slow down the audio as well. Running the above command will give you a slowed down video, but with the audio unchanged, and silence after the original audio runs out.

`ffmpeg -i [input-file] -af "asetrate=[desired sample rate]" [output-file]`

If you wanted to slow down the audio by half, and your original clip has a sample rate of 44100 Hz, you would specify a sample rate of 22050.

You may need to resample your result back up to a 'normal' sample rate to get it to play nice with other clips, depending on what you're doing (44100 and 48000 are common):

`ffmpeg -i [input-file] -af "aresample=[desired sample rate]" [output-file]`

<h4>Chopping a Clip</h4>

[This StackOverflow answer](http://superuser.com/a/141343) helped me figure this one out:

`ffmpeg -i [input-file] -ss [start time] -c copy -t [length of time] [output-file]`

Times can simply be specified in seconds, or in HH:MM:SS.[optional decimal fraction of a second] format.

- - - -

Do note that the `-vf` (video filter) and `-af` (audio filter) flags can be used in the same command - you can slow down the video and the audio in one shot by combining the above `-vf` and `-af` parameters into a nice one-liner, something like:

`ffmpeg -i [input file] -vf "setpts=2*PTS" -af "asetrate=22050" [output-file]`

These are just a couple of initial findings - I hope to make another post or two about the subject in the future as I get deeper into the project. May this inspire you to become a command line auteur!
{'fraction_non_alphanumeric': 0.0730251071647275, 'fraction_numerical': 0.013778322106552358, 'mean_word_length': 4.072204968944099, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2615141', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1802, 'n_words': 1041}