1

I have two videos recorded on an iPhone. One was recorded portrait, the other was recorded landscape:

Portrait video:

ffprobe version 4.2.2 Copyright (c) 2007-2019 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.17)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.2_2 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'IMG_7386.MOV':
  Metadata:
    major_brand     : qt  
    minor_version   : 0
    compatible_brands: qt  
    creation_time   : 2020-04-21T02:00:00.000000Z
    com.apple.quicktime.make: Apple
    com.apple.quicktime.model: iPhone XS
    com.apple.quicktime.software: 13.3.1
    com.apple.quicktime.creationdate: 2020-04-20T22:00:00-0400
  Duration: 00:00:41.00, start: 0.000000, bitrate: 9994 kb/s
    Stream #0:0(und): Video: hevc (Main) (hvc1 / 0x31637668), yuv420p(tv, bt709), 1920x1080, 9782 kb/s, 29.41 fps, 29.97 tbr, 600 tbn, 600 tbc (default)
    Metadata:
      rotate          : 90
      creation_time   : 2020-04-21T02:00:00.000000Z
      handler_name    : Core Media Video
      encoder         : HEVC
    Side data:
      displaymatrix: rotation of -90.00 degrees
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 171 kb/s (default)
    Metadata:
      creation_time   : 2020-04-21T02:00:00.000000Z
      handler_name    : Core Media Audio
    Stream #0:2(und): Data: none (mebx / 0x7862656D), 6 kb/s (default)
    Metadata:
      creation_time   : 2020-04-21T02:00:00.000000Z
      handler_name    : Core Media Metadata
    Stream #0:3(und): Data: none (mebx / 0x7862656D), 0 kb/s (default)
    Metadata:
      creation_time   : 2020-04-21T02:00:00.000000Z
      handler_name    : Core Media Metadata
    Stream #0:4(und): Data: none (mebx / 0x7862656D), 18 kb/s (default)
    Metadata:
      creation_time   : 2020-04-21T02:00:00.000000Z
      handler_name    : Core Media Metadata
Unsupported codec with id 0 for input stream 2
Unsupported codec with id 0 for input stream 3
Unsupported codec with id 0 for input stream 4

Landscape video:

ffprobe version 4.2.2 Copyright (c) 2007-2019 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.17)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.2_2 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'IMG_7168.MOV':
  Metadata:
    major_brand     : qt  
    minor_version   : 0
    compatible_brands: qt  
    creation_time   : 2020-04-13T21:00:14.000000Z
    com.apple.quicktime.make: Apple
    com.apple.quicktime.model: iPhone XS
    com.apple.quicktime.software: 13.3.1
    com.apple.quicktime.creationdate: 2020-04-13T17:00:13-0400
  Duration: 00:00:00.57, start: 0.000000, bitrate: 10809 kb/s
    Stream #0:0(und): Video: hevc (Main) (hvc1 / 0x31637668), yuv420p(tv, bt709), 1920x1080, 10477 kb/s, 30 fps, 30 tbr, 600 tbn, 600 tbc (default)
    Metadata:
      creation_time   : 2020-04-13T21:00:14.000000Z
      handler_name    : Core Media Video
      encoder         : HEVC
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 189 kb/s (default)
    Metadata:
      creation_time   : 2020-04-13T21:00:14.000000Z
      handler_name    : Core Media Audio
    Stream #0:2(und): Data: none (mebx / 0x7862656D), 0 kb/s (default)
    Metadata:
      creation_time   : 2020-04-13T21:00:14.000000Z
      handler_name    : Core Media Metadata
    Stream #0:3(und): Data: none (mebx / 0x7862656D), 0 kb/s (default)
    Metadata:
      creation_time   : 2020-04-13T21:00:14.000000Z
      handler_name    : Core Media Metadata
    Stream #0:4(und): Data: none (mebx / 0x7862656D), 17 kb/s (default)
    Metadata:
      creation_time   : 2020-04-13T21:00:14.000000Z
      handler_name    : Core Media Metadata
Unsupported codec with id 0 for input stream 2
Unsupported codec with id 0 for input stream 3
Unsupported codec with id 0 for input stream 4

The difference between these videos is that the portrait video has displaymatrix: rotation of -90.00 degrees as Side data in Stream #0:0.

I would like to resize these videos with a command like:

ffmpeg -y -i $filename -filter:v scale=-1:720 -vcodec libx265 -crf 30 output.mp4

However, scale=-1:720 only works for the landscape video. To scale the portrait video, I need to change the command to scale=720:-1. Otherwise I get the error:

[libx265 @ 0x7f9ae1009c00] Cannot open libx265 encoder.
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height

Is there a way to adapt the scale filter for any orientation? I would like the long side scaled to 720, whether that's the width or the height.

1 Answer 1

3

There are two issues here.

1) the x265 encoder can't be successfully initialized.
2) due to potential rotation, you can't target the correct side.

For 1., you need to set -2 instead of -1 since the encoder requires even dimensions for the default output pixel format.

For 2. you need conditional expressions in scale, scale=w='if(gt(iw\,ih),720,-2)':h='if(gt(iw\,ih),-2,720)'

4
  • Didn't see the conditional filter expressions documented. Will also have to look up -2. Thanks a lot. Commented Apr 24, 2020 at 16:46
  • Finally getting around to trying this, and maybe there is a mistake somewhere. I am getting [Parsed_scale_0 @ 0x7fa058d0d800] Invalid size 'if(gt(iw,ih)' [AVFilterGraph @ 0x7fa058f218c0] Error initializing filter 'scale' with args 'w=if(gt(iw,ih):flags=bicubic' when using the exact scale command from the answer. Commented May 13, 2020 at 19:18
  • Seems to be an escaping issue like in stackoverflow.com/a/55950639/741970? Commented May 13, 2020 at 19:24
  • The correct expression is scale=w='if(gt(iw\,ih)\,720\,-2)':h='if(gt(iw\,ih)\,-2\,720). Commented May 13, 2020 at 19:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .