how to output gif with same size as input video - ffmpeg

I am following How do I convert a video to GIF using ffmpeg, with reasonable quality?
It gives example:
ffmpeg -i input.flv -i palette.png -filter_complex "fps=10,scale=320:-1:flags=lanczos[x];[x][1:v]paletteuse" output.gif
However I want the gif output to be the same size as video and not 320 as specified here so I removed scale=320:-1 so I have
ffmpeg -i input.flv -i palette.png -filter_complex "fps=10,flags=lanczos[x];[x][1:v]paletteuse" output.gif
When I run that I get:
No such filter: 'flags' Error initializing complex filters.
If I remove:
-filter_complex "fps=10,flags=lanczos[x];[x][1:v]paletteuse"
Then it works but quality of the video is bad. So it seems that I must use a scale for those palette flags to work, how can I get ffmpeg to output gif same size as input video?

Omit the scale filter
By default the output uses the same width and height as the input. The :flags=lanczos was part of the scale filter. So your command will look like:
ffmpeg -i in.flv -i palette.png -filter_complex "fps=10[x];[x][1:v]paletteuse" out.gif

I have figured it out:
ffmpeg -i video.mkv -y -i palette.png -filter_complex "fps=10,scale=iw:ih:flags=lanczos[x];[x][1:v]paletteuse" output_mkv.gif
scale=iw:ih does the trick, same size as input video

Related

FFMPEG: using filters for adding transparent watermark

how can i combine these two lines of code for adding transparent watermark (with Dynamic size)
ffmpeg -i 1.gif -i logo.png -filter_complex "[1]format=rgba,colorchannelmixer=aa=0.5[logo];[0][logo]overlay=(W-w)/2:H-h-5" -c:a copy output.gif
ffmpeg -i 1.gif -i logo.png -filter_complex "[1][0]scale2ref=w=oh*mdar:h=ih*0.1[logo][video];[video][logo]overlay=(W-w)/2:H-h-5" -c:a copy output.gif
i have tried the following code:
ffmpeg -i 1.gif -i logo.png -filter_complex "[1][0]scale2ref=w=oh*mdar:h=ih*0.1[logo][video];[1]format=rgba,colorchannelmixer=aa=0.5[logo];[0][logo]overlay=(W-w)/2:H-h-5" -c:a copy output.gif
i got the following Error:
Filter scale2ref has an unconnected output
Outputs of every filter must be connected to something, and your [video] output pad of scale2ref is not, hence the error.
You need to feed [video] instead of [0] to the overlay filter:
You need to make sure all the filter input & output pads are connected. In your case, format and overlay need to use the output of scale2ref instead of reusing the input streams:
ffmpeg -i 1.gif -i logo.png \
-filter_complex "[1][0]scale2ref=w=oh*mdar:h=ih*0.1[logo1][video];\
[logo1]format=rgba,colorchannelmixer=aa=0.5[logo];\
[video][logo]overlay=(W-w)/2:H-h-5" \
-c:a copy output.gif
[Edit: fixed other labeling issues]

ffmpeg -vg -filtercomplex, create gif with color kway and limited fps

I need to create a gif file with color key (greenscreen) with 10FPS and specified size. I try to combine -vg and -filter_complex:
ffmpeg -i testdatei-c.avi -vf "fps=10,scale=320:-1:flags=lanczos" -filter_complex "[0:v]chromakey=0xFFFFFF,split[v0][v1];[v0]palettegen[p];[v1][p]paletteuse" output.gif
I get the error:
Filtergraph 'fps=10,scale=320:-1:flags=lanczos' 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.
All filters for a stream should be within the same filtergraph, so inside the -filter_complex
ffmpeg -i testdatei-c.avi -filter_complex "[0:v]chromakey=0xFFFFFF,fps=10,scale=320:-1:flags=lanczos,split[v0][v1];[v0]palettegen[p];[v1][p]paletteuse" output.gif

How to bash-script or simplify ffmpeg commands for resizing the watermark, adding it to the video and adding subtitle.ass to the video?

I would like to create hard-subbed video with watermark by using ffmpeg. And I'd like to know how to combine and simplify multiple commands or how to create a bash-script for this purpose.
I've tried searching in stackflow and tested some commands but they didn't work. Here are the comands I'm using.
To detect video width & height:
ffprobe -v quiet -show_entries stream=width,height -of default=noprint_wrappers=1 video_in.mp4
To resize the watermark image: (video width = 1280)
ffmpeg -i watermark.png -y -v quiet -vf scale=1280*0.15:-1 watermark_scaled.png
To add watermark to the video:
ffmpeg -i video_in.mp4 -i watermark_scaled.png -filter_complex "overlay=W-w-5:5" video_marked.mp4
To add .ass subtitle to the video: (it need to be '.ass')
ffmpeg -i video_marked.mp4 -vf ass=subtitle.ass video_final.mp4
You don't need to detect video dimensions. The scale2ref filter can resize input using a reference.
Here's all steps in one command.
ffmpeg -i video_in.mp4 -i watermark.png
-filter_complex "[1][0]scale2ref=iw*0.15:ow/mdar[wm][v];
[v][wm]overlay=W-w-5:5,ass=subtitle.ass"
-c:a copy video_final.mp4

FFmpeg how to add drawtext on filter complex along with watermark and palettegen

thanks to many posts on stack overflow I could come till here: How to add watermark in a gif with ffmpeg? which i can convert mp4 into animated gif with moderate quality and watermark on. But I'd like to add the drawtext too in these lines.
ffmpeg -i in.mp4 -i watermark.png -filter_complex "[0]fps=10,scale=320:-1:flags=lanczos[bg];[bg][1]overlay=W-w-5:H-h-5,palettegen" palette.png
ffmpeg -i in.mp4 -i watermark.png -i palette.png -filter_complex "[0]fps=10,scale=320:-1:flags=lanczos[bg];[bg][1]overlay=W-w-5:H-h-5[x];[x][2]paletteuse=dither=bayer:bayer_scale=3" output.gif
Would it be possible through filter_complex by adding another [ ]? Thanks a lot to everyone
You can add drawtext after overlay.
ffmpeg -i input.mp4 -i watermark.png -filter_complex "[0]fps=10,scale=320:-1[bg];[bg][1]overlay=main_w-overlay_w-10:main_h-overlay_h-10:format=auto,drawtext=text='your text here':fontsize=24:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" output.gif
This will add text centered in video and overlay will be placed in the lower right.

FFmpeg | Option loop not found

The loop option is not working with gif image.
When I'm working with png image the code good.
But when I'm working with animated gif image the error is thrown Option loop not found.
In my example I'm trying to create the video from input image with specific duration.
ffmpeg -loop 1 -t 5 -i 15324210315b56e3a78abe5.png -i watermark.png -filter_complex "[0]scale=trunc(iw/2)*2:trunc(ih/2)*2[v];[v][1]overlay=x=(W-w-10):y=(H-h-10)" output.mp4
Below command is not working
ffmpeg -loop 1 -t 5 -i 15323488345b55c9a2b2908.gif -i watermark.png -filter_complex "[0]scale=trunc(iw/2)*2:trunc(ih/2)*2[v];[v][1]overlay=x=(W-w-10):y=(H-h-10)" output.mp4
GIFs are handled by a seperate demuxer module, not the generic image sequence demuxer. The gif demuxer has a separate option. See command below.
ffmpeg -ignore_loop 0 -t 5 -i 15323488345b55c9a2b2908.gif ...
The python script for gif to video conversion using ffmpeg library
f"/opt/ffmpeglib/ffmpeg -ignore_loop 0 -i {lambda_file_path} -c:v libx264 -t 10 -pix_fmt yuv420p {lambda_output_file_path}

Resources