FFMPEG: How can I get the first frame of an MP4 and maintain its aspect ratio?

Trying to get every mp4 file in a directory, and batch-save out a PNG of its first frame.

I'm using:

ffmpeg -i filename.mp4 -vf "select=eq(n\,0)" -vframes 1 filename.png

When I do this, it correctly saves the first frame of each file, but they're coming out stretched into a square.

Input is 607x1080 Output is 1080x1080

Not all inputs will be the same aspect ration so I can't hard code it. I just want to preserve the aspect ration and dimensions of the original file.

Any ideas?

EDIT: Here's the output:

> ffmpeg -i connecting-to-senses-explainer-1.mp4 -vf "select=eq(n\,0)" -vframes 1 output.png

ffmpeg version 4.4 Copyright (c) 2000-2021 the FFmpeg developers
  built with Apple clang version 12.0.5 (clang-1205.0.22.9)
  configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/4.4_2 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-avresample --enable-videotoolbox
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'connecting-to-senses-explainer-1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 512
    compatible_brands: mp42iso2avc1mp41
    creation_time   : 2021-10-13T22:25:00.000000Z
    encoder         : HandBrake 1.4.2 2021100300
  Duration: 00:00:10.00, start: 0.000000, bitrate: 131 kb/s
  Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1080x1080 [SAR 9:16 DAR 9:16], 128 kb/s, 25 fps, 25 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      creation_time   : 2021-10-13T22:25:00.000000Z
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> png (native))
Press [q] to stop, [?] for help
[swscaler @ 0x1308c0000] No accelerated colorspace conversion found from yuv420p to rgb24.
Output #0, image2, to 'output.png':
  Metadata:
    major_brand     : mp42
    minor_version   : 512
    compatible_brands: mp42iso2avc1mp41
    encoder         : Lavf58.76.100
  Stream #0:0(und): Video: png, rgb24(pc, bt709, progressive), 1080x1080 [SAR 9:16 DAR 9:16], q=2-31, 200 kb/s, 25 fps, 25 tbn (default)
    Metadata:
      creation_time   : 2021-10-13T22:25:00.000000Z
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
      encoder         : Lavc58.134.100 png
frame=    1 fps=0.0 q=-0.0 Lsize=N/A time=00:00:00.04 bitrate=N/A speed= 1.3x
video:110kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

Solution 1:

Your input has non-square pixels (SAR 9:16). ffmpeg tries to preserve aspect ratio when converting. But most image viewers naïvely ignore the Sample Aspect Ratio (SAR) and assume square pixels. Therefore the image looks stretched/squished.

You can use the scale and setsar filters to make square pixels:

ffmpeg -i input.mp4 -vf "scale=iw*sar:ih,setsar=1" -vframes 1 filename.png

If your input already has square pixels the scale filter won't do anything. So there is no harm in using it in a loop for batch converting.