{"id":9725,"date":"2023-10-15T16:38:54","date_gmt":"2023-10-15T14:38:54","guid":{"rendered":"https:\/\/via-internet.de\/blog\/?p=9725"},"modified":"2023-10-15T16:38:59","modified_gmt":"2023-10-15T14:38:59","slug":"ffmpeg-compress-video-files","status":"publish","type":"post","link":"https:\/\/via-internet.de\/blog\/2023\/10\/15\/ffmpeg-compress-video-files\/","title":{"rendered":"FFMPEG | Compress video files"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Compress and Convert MP4 to WMV<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Compress and Convert MP4 to Webm for YouTube, Ins, Facebook<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ffmpeg -i source.mp4 -c:v libvpx-vp9 -b:v 0.33M -c:a libopus -b:a 96k \\&lt;br>-filter:v scale=960x540 target.webm<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Compress and Convert H.264 to H.265 for Higher Compression<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Set CRF in FFmpeg to Reduce Video File Size<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ffmpeg -i input.mp4 -vcodec libx264 -crf 24 output.mp4<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Reduce video frame size to make 4K\/1080P FHD video smaller<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ffmpeg -i input.avi -vf scale=1280:720 output.avi<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Command-line &#8211; resize video in FFmpeg to reduce video size<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ffmpeg -i input.avi -vf scale=852\u00d7480 output.avi<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Resize video in FFmpeg but keep the original aspect ratio<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Specify only one component, width or height, and set the other component to -1, for eample, <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ffmpeg -i input.jpg -vf scale=852:-1 output_852.png<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"convertingwebmtomp4\">Converting WebM to MP4<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ffmpeg -i video.webm video.mp4<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When the WebM file contains VP8 or VP9 video, you have no choice but to transcode both the video and audio.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Video conversion can be a lengthy and CPU intensive process, depending on file size, video and audio quality, video resolution, etc. but FFmpeg provides a series of presets and controls to help you optimize for quality or faster conversions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A note on video quality<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When encoding video with H.264, the video quality can be controlled using a&nbsp;<strong>quantizer<\/strong>&nbsp;scale (crf value, crf stands for Constant Rate Factor) which can take values from&nbsp;<strong>0<\/strong>&nbsp;to&nbsp;<strong>51<\/strong>: 0 being lossless,&nbsp;<strong>23<\/strong>&nbsp;the default and 51 the worst possible. So the lower the value the better the quality. You can leave the default value or, for a smaller bitrate, you can raise the value:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ffmpeg -i video.webm -crf 26 video.mp4<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Video presets<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">FFmpeg also provides several quality presets which are calibrated for a certain encoding speed to compression ratio. A slower preset will provide a better compression. The following presets are available in descending order:&nbsp;<em>ultrafast<\/em>,&nbsp;<em>superfast<\/em>,&nbsp;<em>veryfast<\/em>,&nbsp;<em>faster,&nbsp;<code>fast<\/code><\/em>,&nbsp;<em>medium<\/em>,&nbsp;<em>slow<\/em>,&nbsp;<em>slower<\/em>&nbsp;and&nbsp;<em>veryslow<\/em>. The default preset is&nbsp;<em>medium<\/em>&nbsp;but you can choose a faster preset:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ffmpeg -i video.webm -preset veryfast video.mp4<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Placing the MOOV atom at the beginning<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All MP4 files contain a&nbsp;<code>moov<\/code>&nbsp;atom. The moov atom contains information about the length of the video. If it\u2019s at the beginning it immediately enables a streaming video player to play and scrub the MP4 file. By default FFmpeg places the moov atom at the end of the MP4 file but it can place the mov atom at the beginning with the&nbsp;<code>-movflags faststart<\/code>&nbsp;option like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ffmpeg -i video.webm -movflags faststart video.mp4<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Using Levels and Profiles when encoding H.264 video with FFmpeg<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To ensure the highest compatibility with older iOS or Android devices you will need to use certain encoding profiles and levels. For example a video encoded with the High Profile and Level 4.2 will work on iPhone 5S and newer but not on older iOS devices.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ffmpeg -i video.webm -movflags faststart -profile:v high -level 4.2 video.mp4<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"convertingwebmwithh264videotomp4\">Converting WebM with H.264 video to MP4<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In some rare cases the .webm file will contain H.264 video and Vorbis or Opus audio(for example .webm files created using the&nbsp;<a href=\"https:\/\/addpipe.com\/media-recorder-api-demo\/?ref=deconstruct\">MediaRecorder API on Chrome 52+<\/a>&nbsp;). In such cases you don\u2019t have to re-encode the video data since it\u2019s already in the desired H.264 format (re-encoding is also not recommended since you\u2019ll be loosing some quality in the process while consuming CPU cycles) so we\u2019re just going to copy over the data<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To copy the video data and transcode the audio in FFmpeg you use the&nbsp;<code>-c:v copy<\/code>&nbsp;option:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ffmpeg -i video.webm -c:v copy video.mp4<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Compress and Convert MP4 to WMV Compress and Convert MP4 to Webm for YouTube, Ins, Facebook Compress and Convert H.264 to H.265 for Higher Compression Set CRF in FFmpeg to Reduce Video File Size Reduce video frame size to make 4K\/1080P FHD video smaller Command-line &#8211; resize video in FFmpeg to reduce video size Resize video in FFmpeg but keep the original aspect ratio Specify only one component, width or height, and set the other component to -1, for eample, Converting WebM to MP4 When the WebM file contains VP8 or VP9 video, you have no choice but to transcode both [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9725","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/comments?post=9725"}],"version-history":[{"count":1,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9725\/revisions"}],"predecessor-version":[{"id":9726,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9725\/revisions\/9726"}],"wp:attachment":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media?parent=9725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/categories?post=9725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/tags?post=9725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}