Skip to content

Instantly share code, notes, and snippets.

@lukehedger
Last active December 3, 2023 02:32
Star You must be signed in to star a gist
Embed
What would you like to do?
Compress mp4 using FFMPEG
$ ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 output.mp4
@ArnabXD
Copy link

ArnabXD commented Sep 16, 2021

For webm file, it didn't work as expected.
Input SIze - 9144478
Output Size - 20795660

Probably this is the reason. I noticed this to be true many times

WebM vs MP4: File Size and Quality

But since WebM is specially designed for the Internet, the compression ratio of WebM is generally higher than MP4 in theory, which leads to more original quality loss than MP4. And the file size of WebM is a little bit smaller than MP4.

https://www.winxdvd.com/answers/webm-vs-mp4.htm

@HinaAroraByjus
Copy link

For webm file, it didn't work as expected.
Input SIze - 9144478
Output Size - 20795660

Probably this is the reason. I noticed this to be true many times

WebM vs MP4: File Size and Quality

But since WebM is specially designed for the Internet, the compression ratio of WebM is generally higher than MP4 in theory, which leads to more original quality loss than MP4. And the file size of WebM is a little bit smaller than MP4.

https://www.winxdvd.com/answers/webm-vs-mp4.htm

Thanks ArnabXD,
I have change my approach little.
I was converting the puppeteer stream into mp4 and wanted to stitch the local files and then upload to S3.

Now, I am using AWS Media Converter to perform the same on S3 bucket itself.

@justCatalyst
Copy link

Compressed from 14.1 mb to 4.9 mb. But lost sound( how can i fix it? Any options?

the output for audio is mp2 - maybe you don't have a codec for that?

@abdennour
Copy link

abdennour commented May 27, 2022

WORKS EXCELLENT

ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4

for MacOs, you need to preserve compatibility encoding H.265/HEVC for QuickTime:

ffmpeg -i input.mp4 -c:v libx265 -preset fast -crf 28 -tag:v hvc1 -c:a eac3 -b:a 224k output.mp4

@dotkt
Copy link

dotkt commented Jun 8, 2022

thank you

@Thomashighbaugh
Copy link

ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 out.mp4
3.6 Gb to 556 Mb, great quality 1st_place_medal
ffmpeg -i input.mp4 -s 1280x720 -acodec copy -y output.mp4
3.6 Gb to 62 Mb, quality "good enough"/acceptable +1
ffmpeg -i input.mp4 -vcodec h264 -b:v 1000k -acodec mp3 output.mp4
3.6 Gb to 30 Mb, very shitty quality hankey

Thanks worked beautifully for me on Linux, really appreciate tip!

@deroverda
Copy link

ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 out.mp4
3.6 Gb to 556 Mb, great quality 1st_place_medal
ffmpeg -i input.mp4 -s 1280x720 -acodec copy -y output.mp4
3.6 Gb to 62 Mb, quality "good enough"/acceptable +1
ffmpeg -i input.mp4 -vcodec h264 -b:v 1000k -acodec mp3 output.mp4
3.6 Gb to 30 Mb, very shitty quality hankey

Thanks worked beautifully for me on Linux, really appreciate tip!

tried the 1st_place_medal ffmpeg command AKA ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 out.mp4

before: 49 mb
after: 63 mb

in other words, not gold medal for me. using win 10.

@mcagabe19
Copy link

ffmpeg -i input.mp4 -vcodec libx265 -acodec aac -crf 28 output.mp4
best way (used termux)

@gideonseven
Copy link

gideonseven commented Aug 16, 2022

Commands using Linux with ffmpeg to shrinking big MP4 video file to smaller sizes

ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 out.mp4
3.6 Gb to 556 Mb, great quality 🥇
ffmpeg -i input.mp4 -s 1280x720 -acodec copy -y output.mp4
3.6 Gb to 62 Mb, quality "good enough"/acceptable 👍
ffmpeg -i input.mp4 -vcodec h264 -b:v 1000k -acodec mp3 output.mp4
3.6 Gb to 30 Mb, very shitty quality 💩

using

ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 out.mp4

from 52MB to 28MB

works well for me on windows 11, thank you @tincho

@async-dime
Copy link

thankyou!
using ffmpeg -i input.avi -c:v libx264 -crf 18 output.mp4
input: 1.4 GB
output: 132.2 MB

@ligantx
Copy link

ligantx commented Sep 4, 2022

Commands using Linux with ffmpeg to shrinking big MP4 video file to smaller sizes

  • ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 out.mp4
    • 3.6 Gb to 556 Mb, great quality 🥇
  • ffmpeg -i input.mp4 -s 1280x720 -acodec copy -y output.mp4
    • 3.6 Gb to 62 Mb, quality "good enough"/acceptable 👍
  • ffmpeg -i input.mp4 -vcodec h264 -b:v 1000k -acodec mp3 output.mp4
    • 3.6 Gb to 30 Mb, very shitty quality 💩

good enough worked fine for me..To my "average human" eye, i coudlnt find any real difference with the original video compared side by side (original: 1gb to good enough: 296mb)

Edit:
if you wanna go lower try changinng -b:v value e.g.:
ffmpeg -i input.mp4 -vcodec h264 -b:v 700k -acodec mp3 output.mp4
and if you don't want sound try:
ffmpeg -i input.mp4 -vcodec h264 -b:v 700k -an output.mp4

My experiment:

  • ffmpeg -i input.mp4 -vcodec h264 -b:v 1000k -an output.mp4
    32MB to 3.6MB
  • ffmpeg -i input.mp4 -vcodec h264 -b:v 200k -an output.mp4
    32MB to 742KB

@basicbh
Copy link

basicbh commented Sep 17, 2022

dont forget the FPS,its amazing.

Copy link

ghost commented Sep 28, 2022

I'm passing media from Postman and getting an error.
[WinError 2] The system cannot find the file specified
I'm compressing media like this :
subprocess.run(f'ffmpeg -i {media} -vcodec h264 -acodec {file_type} temp_location/{new_uuid}')

@alyfreym
Copy link

alyfreym commented Jan 4, 2023

How to compress mp4 file with I-frames only?

An I-frame or a Key-Frame or an Intra-frame, how to set this options?

@lnfel
Copy link

lnfel commented Jan 9, 2023

ligantx experiment is the most efficient (tested on macOS), 700k for bitrate seems to be a good measure for maintaining quality while lowering file size:

ffmpeg -i input.mp4 -vcodec h264 -b:v 700k -acodec mp3 output.mp4

@cabiste69
Copy link

i'm gonna add some notes here

  1. in the time of writing this (jan 2023) you don't need to specify h264 as the video codec in ffmpeg when the output is mp4 since that's the default codec
  2. for audio it's better to specify the bitrate rather than the codec, 96Kb is good enough -b:a 96k
  3. not all videos will look good with a video bitrate of 700kb it depends on the resolution, the original codec used and whether there's a lot of movement / particles

I'd recommend that you first let ffmpeg compress it automatically without any arguments ffmpeg -i input.ext output.mp4

  • if the output is not enough then use 2/3 of the original video bitrate with -b:v and keep lowering until you reach a desired size
  • you can lower the resolution with -vf scale=1280:720
  • if you only want to save your video on your pc then use the new mp4 codec 265 -c:v libx265, it has better compression, and just ike 264, nearly lossless. the issue with this is that it's still not widely adapted and sites like discord can't play it

@gideonseven
Copy link

Commands using Linux with ffmpeg to shrinking big MP4 video file to smaller sizes

  • ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 out.mp4
    • 3.6 Gb to 556 Mb, great quality 🥇
  • ffmpeg -i input.mp4 -s 1280x720 -acodec copy -y output.mp4
    • 3.6 Gb to 62 Mb, quality "good enough"/acceptable 👍
  • ffmpeg -i input.mp4 -vcodec h264 -b:v 1000k -acodec mp3 output.mp4
    • 3.6 Gb to 30 Mb, very shitty quality 💩

good enough worked fine for me..To my "average human" eye, i coudlnt find any real difference with the original video compared side by side (original: 1gb to good enough: 296mb)

Edit: if you wanna go lower try changinng -b:v value e.g.: ffmpeg -i input.mp4 -vcodec h264 -b:v 700k -acodec mp3 output.mp4 and if you don't want sound try: ffmpeg -i input.mp4 -vcodec h264 -b:v 700k -an output.mp4

My experiment:

  • ffmpeg -i input.mp4 -vcodec h264 -b:v 1000k -an output.mp4
    32MB to 3.6MB
  • ffmpeg -i input.mp4 -vcodec h264 -b:v 200k -an output.mp4
    32MB to 742KB

thanks, it works.
using || -b:v 1000K || ---- from 2MB to 1MB mp4 file

@drinkmorewaters
Copy link

drinkmorewaters commented Jan 14, 2023

./ffmpeg -i input.mp4 -c:v libx265 -preset ultrafast -crf 28 -c:a aac -b:a 250k output.mp4

This is better for h265, it's fast and should reduce the file size by 50-70% if you want better video, change the ultrafast to fast or change the -crf to a lower number like 24

If you want to change from 4K to 1080P, add -s 1920x1080 after the -i input.mp4

@dwbapst
Copy link

dwbapst commented Jan 18, 2023

./ffmpeg -i input.mp4 -c:v libx265 -preset ultrafast -crf 28 -c:a aac -b:a 250k output.mp4

This is better for h265, it's fast and should reduce the file size by 50-70% if you want better video, change the ultrafast to fast or change the -crf to a lower number like 24

If you want to change from 4K to 1080P, add -s 1920x1080 after the -i input.mp4

This worked pretty well for me -- converted a 323 MB Zoom recording to a 81 MB file. without reducing resolution, is there anything more one can do to further shave off the file size?

@clairechen7
Copy link

Commands using Linux with ffmpeg to shrinking big MP4 video file to smaller sizes

  • ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 out.mp4
    • 3.6 Gb to 556 Mb, great quality 🥇
  • ffmpeg -i input.mp4 -s 1280x720 -acodec copy -y output.mp4
    • 3.6 Gb to 62 Mb, quality "good enough"/acceptable 👍
  • ffmpeg -i input.mp4 -vcodec h264 -b:v 1000k -acodec mp3 output.mp4
    • 3.6 Gb to 30 Mb, very shitty quality 💩

Thanks for sharing, really helpful!

@AD5GB
Copy link

AD5GB commented Jun 20, 2023

None of these have done anything, but give me a 30-35% INCREASE in size. I'm still determined to find a solution but attempts to compress this video by ANY means results in similar results.....

@cabiste69
Copy link

@AD5GB as i said before here, it depends on what video you have, so you need to experiment a bit

@Sothinkmedia
Copy link

A DCV file is a special encrypted format used by the video-sharing site and download service provided by DMM.com.

This file format aims to address issues such as copyright protection and illegal uploading. Additionally, DCV files offer high-quality images and sound, resolving problems like choppiness, image degradation, and sound loss during streaming playback.

However, DCV files cannot be played using anything other than dedicated players or download software; thus, they need to be converted to the MP4 format.

Converting DMM video DCV files to MP4 requires the use of dedicated software, many of which are available free of charge.

In this article, we will guide you on how to convert DMM video DCV files to MP4.
https://sothinkmedia.com/ja/convert-dmm-dcv-files

@aoi306
Copy link

aoi306 commented Oct 8, 2023

Very good! Using it, reduced 86mb to 64mb. thank you all. I also summarized some other methods, I hope it can help everyone https://vidon.me/compress-mp4

@Sothinkmedia
Copy link

Myfans(マイファンズ)は、有料のコンテンツプラットフォームで、主にアダルトコンテンツを提供するために使用されています。

ユーザーは、自分自身やその他の人が作成した独自の写真、ビデオ、および記事を購読することができます。サブスクリプションモデルを採用しており、コンテンツ提供者は購読者からの収入の一部を得ることができます。

ただし、プライバシーなどの理由から、Myfansではダウンロード機能は提供されていません。

そこで、本記事では、iPhone、Windows、MacなどのデバイスでMyfansダウンロードする方法についてまとめます。

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