Tuesday, May 7, 2024

FFmpeg : bulk compress videos

The phones are getting bigger and there camera's more so. Now videos are getting recorded in 4K with bitrate around 20Mbps. Now if you wish to store them for long term, it takes a lot of space on the phone which normally has fixed storage. Even you backup on a hard disk, it grows with time. So ideally just compress it with no perceptible loss in quality.

To do so (on Windows):

  1. Insall FFmpeg : https://www.wikihow.com/Install-FFmpeg-on-Windows 
  2. Copy all videos into a folder from you phone to laptop
  3. run a batch script:
    for %%c in (*.mp4) do ( 
     ffmpeg -i %%c  -vcodec libx265 -crf 28 out\%%c
    )
  4. It will convert all the videos (mp4 files) one by one and the result will be stored in a out folder.
  5. Try to view some videos in the out folder and once satisfied, correct the created and modified date with the below powershell script:

    $modifyfiles = Get-ChildItem -force *.mp4| Where-Object {! $_.PSIsContainer}
    foreach($object in $modifyfiles)
    {
       $object.CreationTime=[datetime]::ParseExact($object.BaseName.Substring(4),"yyyyMMdd_HHmmss",$null)
       $object.LastWritetime=[datetime]::ParseExact($object.BaseName.Substring(4),"yyyyMMdd_HHmmss",$null)
    }
  6. Now copy the videos back to your phone (and your local hard disk backup as required)
  7.  

No comments: