ffmpeg Tips

ffmpeg examples

Before uploading videos, I crop the video using ffmpeg.

■Install ffmpeg
      $ brew install ffmpeg
■Check video information
      // ffprobe -i [input file]
      $ ffprobe -i PoTone_Outline_FA2020week2.mp4 
      Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'PoTone_Outline_FA2020week2.mp4':
      Metadata:
        major_brand     : qt  
        minor_version   : 0
        compatible_brands: qt  
        creation_time   : 2020-02-10T10:21:25.000000Z
      Duration: 00:00:14.04, start: 0.000000, bitrate: 6490 kb/s
        Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p,
          1024x768 [SAR 1:1 DAR 4:3], 6487 kb/s, 24 fps, 24 tbr, 600 tbn, 
          1200 tbc (default)
        Metadata:
          creation_time   : 2020-02-10T10:21:25.000000Z
          handler_name    : Core Media Video
          encoder         : H.264

Original video by ffmpeg (1024:768, 11.3MB)

■Scale video
      // ffmpeg -i [input file] 
      //        -vf "scale=[width]:[height], (*)
      //             pad=[width]:[height]:[offset x]:[offset y]:[color]"
      //        -c:v [video codec]
      //        -q:v [quality of image from 1(high) to 32(low)]
      //        [output file]
      $ ffmpeg -i PoTone_Outline_FA2020week2.mp4 
                  -vf "scale=768:768,pad=1024:768:128:0:black" 
                  -c:v "mpeg4"
                  -q:v 1
                  PoTone_Outline_FA2020week2_1_768-768_wpad_q1_org.mp4
      
      // If you want to keep aspect rate of the video, specify "scale=[width]:-1".
      $ ffmpeg -i w07_33_mil.MOV 
                  -vf "scale=768:-1" 
                  w07_33_mil_768_-1.mp4

Scaled with pad option by ffmpeg (720:720 in pad(1028:768) 15.4MB)

■Crop video
      // ffmpeg -i [input file] 
      //       -vf "crop=[width]:[height]:[postion x]:[position y]"
      //       [output file]
      $ ffmpeg -i PoTone_Outline_FA2020week2.mp4 \
                -vf "crop=720:720:140:0" \ 
                PoTone_Outline_FA2020week2_crop_720_from_x140.mp4

Cropped video by ffmpeg (720:720, 3.3MB)

■Rotate video
      // Rotate right (90 degree)
      // ffmpeg -i [input file] -vf transpose=1 -metadata:s:v:0 rotate=0 
      //           [output file]


      // Rotate left (270 degree)
      // ffmpeg -i [input file] -vf transpose=2 -metadata:s:v:0 rotate=0 
      //           [output file]
      $ ffmpeg -i IMG_1381.MOV -vf transpose=2 -metadata:s:v:0 rotate=0 \
                w07_build_timewraps.mov

■Resize video

For making video file < 10MB, I tried the command of "variable bit rate" like ffmpeg -i input_video -vcodec libx264 -crf 25 -preset medium -vf scale=-2:1080 -acodec libmp3lame -q:a 4 -ar 48000 -ac 2 output_video.mp4. However, the output file reaches to 19MB. Then I tried -fs 10MB option.

However, "-fs 10M" option stops renderring when it reaches to the file size (it might be limited in some cases). This means the video timewindow is cut down, so it looks not to be useful for shrink file size without cut timewindow of the video. .

Instead, following command of fixing bitrate works.

      //ffmpeg -i input_video -vcodec libx264 -b:v 1000k -vf scale=-2:1080 -acodec mp2 -b:a 256k -ar 48000 -ac 2 output_video.mp4  
      $ ffmpeg -i presentation_org.mp4 -vcodec libx264 -b:v 1000k -vf scale=-2:1080 -acodec mp2 -b:a 256k -ar 48000 -ac 2 presentation.mp4
      $ ls -la 
      -rw-r--r--@ 1 user1  group1  41727052  5  5 21:34 presentation_org.mp4
      -rw-r--r--@ 1 user1  group1   6735088  5  6 00:18 presentation.mp4

As of 15 July, my local client did not work on above command. Instead, following command worked for me.

ffmpeg -i PoTone_1min_ver.2.mp4 -vcodec libx264 -b:v 1000k -vf scale=-2:1080 -acodec aac -b:a 256k -ar 48000 -ac 2 presentation.mp4
■Example from academy class page
variable bit rate 1080p MP3:

   ffmpeg -i input_video -vcodec libx264 -crf 25 -preset medium -vf scale=-2:1080 -acodec libmp3lame -q:a 4 -ar 48000 -ac 2 output_video.mp4

fixed bit rate 1080p MP2:

   ffmpeg -i input_video -vcodec libx264 -b:v 1000k -vf scale=-2:1080 -acodec mp2 -b:a 256k -ar 48000 -ac 2 output_video.mp4

no audio:

   ffmpeg -i input_video -vcodec libx264 -b:v 1000k -vf scale=-2:1080 -an output_video.mp4

crop size (width:height:xoffset:yoffset):

   ffmpeg -i input_video -vf crop=1500:800:200:100 -vcodec libx264 -b:v 1000k -an output_video.mp4

trim time (-ss start time, -t duration):

   ffmpeg -i input_video -vcodec libx264 -b:v 1000k -an -ss 00:00:10 -t 00:00:10 output_video.mp4

mix audio and video:

   ffmpeg -i input_video -vcodec libx264 -b:v 1000k -vf crop=1120:876:0:100 -i input_audio -acodec mp2 -b:a 256k -ar 48000 -ac 2 -ss 00:00:20 -t 00:00:20 output_video.mp4

crop, pan, composite:

   ffmpeg -i input_video_1 -i input_video_2 -filter_complex '[1:v]crop=175:95:930:860[cropout];[cropout]scale=350:190[scaleout];[0:v][scaleout]overlay=10:10[outv]' -map '[outv]' -vcodec libx264 -b:v 1000k -map 0:a -acodec mp2 -b:a 256k -ac 2 -t 00:00:05 output_video.mp4

References