Skip to content

Instantly share code, notes, and snippets.

@adamsoderstrom
Last active December 1, 2021 19:45
Show Gist options
  • Save adamsoderstrom/493288e16946434878a37766a184a7d1 to your computer and use it in GitHub Desktop.
Save adamsoderstrom/493288e16946434878a37766a184a7d1 to your computer and use it in GitHub Desktop.

Making Quicktime recordings smaller

Problem

When recording a Quicktime video (either through camera or screen recording), the resulting file becomes way bigger than it needs to be.

Solution

Convert the file from .mov to .mp4

Quicktime exports the recording to a .mov by default and it's not possible to alter that setting.

I created a screen recording, named input.mov, which lasted 28 seconds and ended up with a file size of 16.8MB (which is too big, according to me).

Converting the video, using ffmpeg, with the script below:

ffmpeg -i input.mov -vcodec libx264 -crf 24 output.mp4

source

Resulted in a file size of 5.4MB, which seems neat.

Automatically convert the screen recordings upon saving

~/Desktop is the default save location of screen recordings (don't know if this possible to edit in MacOS preferences). I found fswatch a good solution for this.

fswatch listens to all file events triggered in target directory "by default" (fswatch .). Piping to other programs after triggered events are also possible:

$ fswatch -0 path | while read -d "" event \
	do \
	// do something with ${event}
	done

But how do we distinguish the events that in conjunction with a screen recording from Quicktime?

...Well, i had a look on what the included event flags are in the events from QuickTime. To show the event flags, i ran fswatch -x .. The following output was done after completing a screen recording:

$ ~/Desktop/ fswatch -x .
~/Desktop/Skärminspelning 2021-12-01 kl. 16.05.47.mov IsFile Renamed AttributeModified
~/Desktop/Skärminspelning 2021-12-01 kl. 16.05.47.mov IsFile AttributeModified

Skärminspelning means Screen recording in Swedish

image

Hm... interesting! It looks like saving a screen recording resulted in two events.

Since we want to convert the file, it would be a good thing to trigger the file conversion on only the last event, to ensure that the file saving process is completed.

I tested the following script:

fswatch -0 -x --event AttributeModified --include \.mov$ .

But it was not sufficient, since it would trigger to both of the events we saw earlier. It isolated the printed events to only be .mov files though and that's great!

I wanted to distinguish the two compositions of event flags from each other. One great thing i found, was the Numeric Event Flags (-n).

I ran fswatch -0 -n --include \.mov$ . and got the following:

~/Desktop/Skärminspelning 2021-12-01 kl. 16.19.27.mov 592
~/Desktop/Skärminspelning 2021-12-01 kl. 16.19.27.mov 576

Look! We got two different sums of the event "compositions"! This is a bit error prone, but for our narrow use case (*.mov), i think this i sufficient enough. Running touch test.mov even resulted in a 514, so that's great!

So, the file conversion should trigger:

  • when a file event happens
    • with a event sum of 576
    • regarding a .mov file
    • in ~/Desktop

Could / Should we narrow it even further?

Heck yeah!

In fact, running touch test.mov when an already existing test.mov caused a 576 aswell, so we can not be done!

Running ffprobe input.mov showed us the following line:

...
major_brand: qt
...

Ooh! Maybe that's sufficient? Think qt implies QuickTime.

Either way, constructed this beautiful line here:

ffprobe -v error  -hide_banner -select_streams v:0 -show_entries format_tags=major_brand -of default=noprint_wrappers=1 input.mov | grep "qt"

source

Which resulted in:

TAG:major_brand=qt

+ an exit code of 0! Yay!

Tying it together

So now that we have our two scripts:

  • Converting .mov to .mp4
  • Listen for created QuickTime recordings on ~/Desktop

We should be able to tie them together!


The results (with a few modification from discoveries here) could be viewed in this repo.

Thanks for reading!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment