Search code examples
ffmpegpixelvideo-processingffmpeg-php

How can one use ffmpeg to position a video exactly at a specific pixel position in jpeg?


For example,

  • there is a jpeg with dimensions 1900x1000 and it has a frame (a rectangle) with pixel coordinates 490x100 and 1400x500
  • a video with resolution 720p needs to be placed within this rectangle

Could you pls share the ffmpeg command to concatenate the jpeg and video? I tried -hstack and -xstack filters. But, couldn't get the video inside the rectangle.

TIA


Solution

  • Your rectangle has a size of 910x400, so you have to downscale the 1280x720 video to fit. But the aspect ratios do not match. So you need to also pad or crop the 1280x720 video to properly fit inside 910x400 if you want to preserve the aspect ratio.

    Combine these answers:

    Crop to fit

    ffmpeg -i image.jpg -i video.mp4 -filter_complex "[1]scale=910:400:force_original_aspect_ratio=increase,crop=910:400[fg];[0][fg]overlay=x=490:y=100,format=yuv420p" -c:a copy -movflags +faststart output.mp4
    

    Pad to fit

    ffmpeg -i image.jpg -i video.mp4 -filter_complex "[1]scale=910:400:force_original_aspect_ratio=decrease,pad=910:400:-1:-1[fg];[0][fg]overlay=x=490:y=100,format=yuv420p" -c:a copy -movflags +faststart output.mp4
    

    Squish/stretch to fit

    This will ignore the aspect ratio and will result in a distorted image.

    ffmpeg -i image.jpg -i video.mp4 -filter_complex "[1]scale=910:400,setsar=1[fg];[0][fg]overlay=x=490:y=100,format=yuv420p" -c:a copy -movflags +faststart output.mp4