0%

Compress Native System Screen Recording

Compress Native System Screen Recording

Problem Statement

The native screen recording in Windows/MacOS has very high quality, so that the video are usually too large to share with others. For example, a 2-hour screen recording on MacOS consumes 14 GB memory!

However, we noticed that the screen recording from zoom/tencent meeting is very light-weight. A 2-hour screen recording costs only hundreds of megabytes!

So, if we want to share the screen recording from native system to others, can we convert it to the one like the zoom? It has smaller file size and the video is quality is enough for watching.

Observation & Analysis

We get two videos, from Tencent Meeting (tencent.mp4) and Windows Native recording (windows.mp4) respectively:

tencent.mp4 windows.mp4
Duration 6.7s 11.6s
File Size 261KB 11675KB

We found that the size of tencent.mp4 is far smaller (~20x) than the windows.mp4! Let’s analysis some details by the ffprobe provided by ffmpeg:

1
2
3
4
# Description: Get the information of the video file using ffprobe
ffprobe -v error -show_format -show_streams tencent.mp4

ffprobe -v error -show_format -show_streams windows.mp4

And here are some key observations comparison:

  1. Codec Profile:
    • tencent.mp4 uses the H.264 High Profile, which is suitable for high resolution and high compression efficiency.
    • windows.mp4 uses the H.264 Main Profile, which typically has slightly lower compression efficiency compared to the High Profile.
  2. Frame Rate:
    • tencent.mp4 has a relatively low frame rate (approximately 15 fps), which helps to reduce file size.
    • windows.mp4 has a higher frame rate (60 fps), which significantly increases the file size.
  3. Bit Rate:
    • tencent.mp4 has a very low bit rate (184243 bps), which greatly reduces file size but may affect video quality.
    • windows.mp4 has a very high bit rate (7457076 bps), which ensures higher video quality but also results in a much larger file size.
  4. Audio Settings:
    • tencent.mp4 has mono audio and a lower sampling rate (32000 Hz), which helps to reduce file size.
    • windows.mp4 has stereo audio and a higher sampling rate (48000 Hz), which increases file size.

Solution

From the analysis above, we come up the plan to reduce the video file size. We use ffmpeg to convert the videos. The following Python script helps making the ffmpeg commands:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Description: This script compresses a video file using ffmpeg with some preset parameters. 
import os, pyperclip

input_file = 'input.mp4'
output_file = 'compressed_output.mp4'

# H264 (libx264) encoder
profile = 'high' # H.264 encoding profile (e.g., 'high', 'main', 'baseline')
crf = 23 # Constant Rate Factor, the lower the value, the higher the quality (range: 0-51)
bitrate = '184k' # Video bitrate (e.g., '500k', '1000k')
framerate = 15 # Frame rate (fps)
vcodec = 'libx264' # Video codec (e.g., 'libx264', 'libx265')

cmd = f'ffmpeg -i {input_file} -vcodec {vcodec} -profile:v {profile} -crf {crf} ' \
f'-preset fast -b:v {bitrate} -r {framerate} -pix_fmt yuv420p -acodec aac ' \
f'-b:a 128k -ar 32000 -ac 1 {output_file}'

pyperclip.copy(cmd)
print(cmd)
os.system(cmd)

The output command example:

1
ffmpeg -i input.mp4 -vcodec libx264 -profile:v high -crf 23 -preset fast -b:v 184k -r 15 -pix_fmt yuv420p -acodec aac -b:a 128k -ar 32000 -ac 1 compressed_output.mp4

To convert the file, we apply the high profile, a low crf, a low bitrate and framerate. You may export the video and check whether the video is over-compressed.

Apple Silicon Acceleration

For Apple Silicon users, we may apply the Apple’s own H264 encoding library for acceleration. The command may look like this:

1
2
3
4
5
6
# Apple's H264 encoder
framerate = 15 # Frame rate (fps)
quality = 10 # Quality
cmd = f'ffmpeg -i {input_file} -vf "scale=1920:-2,fps={framerate}" -c:v h264_videotoolbox ' \
f'-q:v {quality} -acodec copy ' \
f'{output_file}'

The output command example:

1
ffmpeg -i input.mp4 -vf "scale=1920:-2,fps=15" -c:v h264_videotoolbox -q:v 10 -acodec copy compressed_output.mp4