ffmpeg transpose source and scale watermark to fit on different video sizes - ffmpeg

I'm using already using the line below to transpose the source, but I'm facing a problem of the watermark being out of proportion when the video resolution is different
How can I make watermark fit on different video sizes?
ffmpeg -i input -i watermark.png -filter_complex "transpose=1,overlay=-40:300" -vb 370k -minrate 300k -maxrate 350k -bufsize 350k -aspect "720:1280" -s "360x640" -c:v libx264 -profile:v "Main" -level "3.1" -r 25 -g 25 -keyint_min 50 -x264opts "keyint=50:min-keyint=50:no-scenecut" -c:a aac -strict experimental -b:a 32000 -ar 32000 -ac 1 output

The scale2ref filter is meant for this use case.
-filter_complex "[0]transpose=1[v];[1][v]scale2ref=oh*mdar:ih/8[w][v];[v][w]overlay=X:Y"
The watermark's height will be resized to 1/8th the height of the video. The width will be proportionally resized.

Related

While ffmpeg is recording, I want it to create a smaller and lower quality video

Currently I am using this...
ffmpeg -video_size 1920x1080 -framerate 1 -f x11grab -i :0.0+0,0 -f pulse -ac 2 -i default -t 00:00:10 Output.mkv
While ffmpeg is recording a video, I want it to significantly reduce both the size and quality compared to the ffmpeg command above.
In case you are curious, I am recording brief quality assurance videos to ensure a simple little web scraper I wrote in Python is scraping data properly (specifically, that it is clicking on a particular button, at a particular time, on a particular web page). My Python script triggers the command above to start recording my screen a few seconds before my Python script is supposed to click on that button.
Of course, to verify a button on a web page had been clicked on, low quality video resolution would normally suffice.
For libx264/libx265 the most important option to reduce both the size and quality is -crf. This option controls quality. A value of 51 provides the worst quality. If it's too terrible then use a lower number.
ffmpeg -video_size 1920x1080 -framerate 1 -f x11grab -i :0.0+0,0 -f pulse -channels 2 -i default -t 00:00:10 -c:v libx264 -crf 51 -c:a libopus Output.mkv
See FFmpeg Wiki: H.264.
For significantly reduce both the size and quality of Output.mkv you can use the next ffmpeg configuration:
crop: iw-(cut width in pixels):ih-(cut heigth in pixels)
scale: to set the ratio and resolution (example 1700x800)
crf: to set quality, where 0 is lossless, 23 is default, and 51 is worst possible. A lower value is a higher quality and a subjectively sane range is 18-28. Consider 18 to be visually lossless
bitrate value: -b:v value -minrate value and -maxrate max value (example -b:v 4000K -minrate 2000K -maxrate 6000K)
preset: in theory slow is best quality/size, and you can probe ultrafast
superfast
veryfast
faster
fast
medium – default preset
slow
slower
veryslow
Cut a part of video, changue the resolution, changue crf, and bitrate, first only video, then you can add audio in other work, dont mix, repeat, is very important, encoding/decoding audio and video separate, then you can mix all, but first the video like in this example
ffmpeg -i Output.mkv -map 0:v -vf crop=iw-150:ih-85,scale=ih*16/9:ih,scale=1072:732,setsar=1 -c:v libx264 -crf 17 -b:v 4000K -maxrate 6000K -bufsize 4M -movflags -faststart -preset veryfast -dn video-new.mkv

I am using ffmpeg for overlaying a image on live stream. How can i scale accoring to the widht of my screen so that it fits completely

I am scaling the output from complex filter to different standard resolutions using the -s flag but the result is that the video does not fit completely into my output screen. How can i scale the different outputs dynamically according to the screen. Here is my command.
ffmpeg -i rtmp://127.0.0.1:1935/show/$2 -i $overlayUrl -filter_complex "[1][0]scale2ref=iw:ih[ovr][base];[base][ovr] overlay=0:0, split=4[a][b]" -async 1 -vsync -1 -map 0:a -map "[a]" -c:v libx264 -c:a aac -b:v 256k -b:a 32k -s 640x360 -tune zerolatency -r 60 -preset veryfast -crf 23 -f flv rtmp://$rtmpoutput/$2_low -map 0:a -map "[b]" -c:v libx264 -c:a aac -b:v 768k -b:a 96k -s 640x480 -tune zerolatency -r 60 -preset veryfast -crf 23 -f flv rtmp://$rtmpoutput/$2_mid code here
Your output has to match the aspect ratio of the screen resolution. Only practical solution is to provide a best guess to match the most common screen size of your viewers. The others will have letterbox/pillarbox to fit the screen.

FFMPEG: Combine "Create video from images" + scale to x + add audio + overlay logo

I´m working on a webcam-project. It is for generating timelapse videos of sunset/sundown.
I´m using a raspberrypi to generate them with gphoto2 + DSLR.
At the end of the day the images should get to an video, with audio and an overlay logo.
And it should be scaled to 1920 pixel.
I got a nice solution an it worked.
Producing the timelapse video an scale it:
ffmpeg -y -framerate 25 -start_number 0000001 -i /var/www/html/webcam/2020-01-05_bilder/%7d.jpg -vf scale=1920:-1 -pix_fmt yuv420p /var/www/html/webcam/2020-01-05-tag-output-1920.mp4
Taking the output of (1) and add an overlay-logo, add audio
ffmpeg -y -i '/var/www/html/webcam/2020-01-05-tag-output-1920.mp4'
-i '/var/www/html/webcam-scripts/graphics/logo.png'
-i '/var/www/html/webcam-scripts/sounds/chill_time_5.mp3'
-shortest -filter_complex '[1][0]scale2ref=h=ow/mdar:w=iw/6[#A logo][liebfrauen]; [#A logo]format=argb,colorchannelmixer=aa=0.95[#B logo transparent]; [liebfrauen][#B logo transparent] overlay=(main_w-w)-(main_w*0.05):(main_h-h)-(main_h*0.01)'
-c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -c:a aac -strict -2
'/var/www/html/webcam/2020-01-05-tag-1920.mp4
I tried to combine both actions, but I get an error:
ffmpeg -y -framerate 25 -start_number 0000001 -i '/var/www/html/webcam/2020-01-05_bilder/%7d.jpg' -vf scale=1920:-1 -pix_fmt yuv420p -i '/var/www/html/webcam-scripts/graphics/logo.png' -i '/var/www/html/webcam-scripts/sounds/chill_time_5.mp3' -shortest -filter_complex '[1][0]scale2ref=h=ow/mdar:w=iw/6[#A logo][liebfrauen]; [#A logo]format=argb,colorchannelmixer=aa=0.95[#B logo transparent]; [liebfrauen][#B logo transparent] overlay=(main_w-w)-(main_w*0.05):(main_h-h)-(main_h*0.01)' -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -c:a aac -strict -2 '/var/www/html/webcam/2020-01-05-tag-1920.mp4'
Error: Filtergraph 'scale=720:-1' was specified through the -vf/-af/-filter option for output stream 0:0, which is fed from a complex filtergraph.
-vf/-af/-filter and -filter_complex cannot be used together for the same stream.
Isn`t it possible to combine these inputs and scale it? Or ... Where is my misunderstanding?
Don't mix -vf and -filter_complex. Do all filtering in one filtergraph.
ffmpeg -y -framerate 25 -i '/var/www/html/webcam/2020-01-05_bilder/%7d.jpg' -i '/var/www/html/webcam-scripts/graphics/logo.png' -i '/var/www/html/webcam-scripts/sounds/chill_time_5.mp3' -filter_complex '[0]scale=1920:-2[v0];[1][v0]scale2ref=h=ow/mdar:w=iw/6[#A logo][liebfrauen]; [#A logo]format=argb,colorchannelmixer=aa=0.95[#B logo transparent]; [liebfrauen][#B logo transparent] overlay=(main_w-w)-(main_w*0.05):(main_h-h)-(main_h*0.01),format=yuv420p' -c:v libx264 -crf 18 -preset slow -c:a aac -shortest '/var/www/html/webcam/2020-01-05-tag-1920.mp4'
No need for -strict -2. It does nothing for modern ffmpeg.
I replaced -pix_fmt yuv420p with format=yuv420p so it is more organized.
-start_number 0000001 is not needed because 1 is the default.

Efficient command line to crop a video, overlay another crop from it and scale the result with ffmpeg

I need to convert many videos in such a way that I take 2 different crops from each frame of a single video, stack them one over the other and scale down the result, creating a new smaller video.
I want to convert this fullHD frame (two crop areas are marked red) to this small stacked frame.
Right now I use the following code:
ffmpeg -i "video.mkv" -filter:v "crop=560:416:0:0" out1.mp4
ffmpeg -i "video.mkv" -filter:v "crop=560:384:1060:128" out2.mp4
ffmpeg -i out1.mp4 -vf "movie=out2.mp4[inner]; [in][inner] overlay=0:32,scale=280:208[out]" -c:v libx264 -preset veryfast -crf 30 result.mp4
It works but it is very inefficient and requires temporary files (out1 and out2). And the problem is I have over 100.000 of such videos (they are big and stored on a NAS and not directly on my computer's HDD). Converting all of them with a Windows batch script (for loop) will take...48 days. Can you help me to optimize the script?
Use the crop, vstack, scale, and format filters:
ffmpeg -i input.mkv -filter_complex "[0:v]crop=560:24:0:0[top];[0:v]crop=560:384:1076:128[bottom];[top][bottom]vstack,scale=280:-2[out]" -map "[out]" -c:v libx264 -preset veryfast -crf 30 -movflags +faststart result.mp4
If you want to complicate it somewhat for faster filtering (maybe) then you can try scaling first:
ffmpeg -i input.mkv -filter_complex "[0:v]scale=iw/2:-1,split[v0][v1];[v0]crop=560/2:24/2:0:0[top];[v1]crop=560/2:384/2:1076/2:128/2[bottom];[top][bottom]vstack[out]" -map "[out]" -c:v libx264 -preset veryfast -crf 30 -movflags +faststart result.mp4
You'll have to experiment to see which is fastest for you.

Rescaling and slowing down a movie at the same time with ffmpeg

I would like with ffmpeg to slow down a movie I am creating using the flag:
-filter:v "setpts=2.0*PTS"
However the height of my still images is not divisible by 2, so to avoid the error: height not divisible by 2 (1238x833), I am using the flag:
-vf scale="trunc(iw/2)*2:trunc(ih/2)*2"
(I also tried -vf scale=1238:-2).
When I do this the film is generated but it isn't slowed down, like if the -filter:v "setpts=2.0*PTS" wasn't there.
Is there something particular to do in order to have both option working at the same time?
Here is the complete command I am using:
ffmpeg -an -i ./movie/cphmd1.%05d.ppm -vcodec libx264 -pix_fmt yuv420p -b:v 5000k -r 24 -crf 18 -filter:v "setpts=2.0*PTS" -vf scale="trunc(iw/2)*2:trunc(ih/2)*2" -preset slow -f mp4 cphmd1_slower.mp4
Many thanks in advance!
Multiple filters acting on the same input, in series, have to be chained together. So,
ffmpeg -an -i ./movie/cphmd1.%05d.ppm -vcodec libx264 -pix_fmt yuv420p -b:v 5000k -r 24 -crf 18 -vf "setpts=2.0*PTS,scale=trunc(iw/2)*2:trunc(ih/2)*2" -preset slow -f mp4 cphmd1_slower.mp4

Resources