Skip to content

Commit 5cfece5

Browse files
committed
Video to GIF converter v1.0.0
Signed-off-by: Nabi <[email protected]>
1 parent 6e332e6 commit 5cfece5

File tree

5 files changed

+154
-13
lines changed

5 files changed

+154
-13
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
/.idea
2-
sample.mp4
3-
sample.gif

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,51 @@ A batch script for convert video to GIF files by FFmpeg.exe on Windows
77
* Make sure the `ffmpeg.exe` and `video2gif.bat` are on your systems path.
88

99
## Usage
10-
Anywhere you can use it be like this example:
10+
**Method #1:** Anywhere you can use it by this command in `cmd`:
1111
```
1212
video2gif myvideo.mp4
1313
```
1414
Then you have `myvideo.gif` in current directory.
1515
If `myvideo.gif` there is existed, question from you for overwrite it.
1616

17+
**Method #2:** You can just drag and drop `.gif` file on the `video2gif.bat` file.
18+
19+
## Advance Usage
20+
21+
```
22+
video2gif SOURCE_FILE [OPTIONS]
23+
```
24+
25+
##Options:
26+
```
27+
SOURCE_FILE Source video file name for convert
28+
also you can drag and drop source video file to this batch file directly
29+
-o Output destination .GIF file name
30+
if not set it, use video source file name with new .gif extension
31+
-y Overwrite destination file
32+
if not set it, appears prompt for overwrite destination file
33+
-f Frame per second (fps)
34+
default: 15
35+
-w Width scale of destination gif file
36+
in pixel unit and (-1) for use original width, default: -1
37+
-s Start time of video source for crop
38+
in second or time format (ex. 1:12), if set it, so must be set (-d) param
39+
-d Duration time of video source for crop
40+
in second, if set it, so must be set (-s) param
41+
-c Maximum number of colors to the palette
42+
must be (<=256), default: 256
43+
-q Quality of destination gif file
44+
must be a number between 1(low) to 6(high), default: 5
45+
```
46+
47+
## Examples:
48+
```
49+
video2gif sample.mp4
50+
video2gif sample.mp4 -y -w 60 -q 1 -f 10
51+
video2gif sample.mp4 -o new_file.gif -y -w 100 -f 10 -s 10 -d 5
52+
video2gif sample.mp4 -s 0:30 -d 20 -c 128 -q 6
53+
video2gif sample.mp4 -s 1:16.5 -d 8.3
54+
```
55+
1756
## Tips
1857
Special thanks to [this article](http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html).

sample.gif

99 KB
Loading

sample.mp4

190 KB
Binary file not shown.

video2gif.bat

Lines changed: 114 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,114 @@
1-
@echo off
2-
rem by: nabi karamalizadeh <[email protected]>
3-
rem description: video to gif converter
4-
rem version: 0.0.1
5-
rem license: The MIT License (MIT)
6-
set arg1=%1
7-
set arg2=%arg1:~0,-4%
8-
ffmpeg -y -i %arg1% -vf fps=15,scale=-1:-1:flags=lanczos,palettegen %TEMP%\palette.png
9-
ffmpeg -i %arg1% -i %TEMP%\palette.png -filter_complex "fps=10,scale=-1:-1:flags=lanczos[x];[x][1:v]paletteuse" %arg2%.gif
10-
del /f %TEMP%\palette.png
1+
@ECHO OFF
2+
REM By: Nabi KaramAliZadeh <[email protected]>
3+
REM Description: video to gif converter
4+
REM Version: 1.0.0
5+
REM Url: https://github.com/NabiKAZ/video2gif
6+
REM License: The MIT License (MIT)
7+
8+
SETLOCAL
9+
10+
SET "input="
11+
SET "output="
12+
SET "overwrite="
13+
SET "fps="
14+
SET "width="
15+
SET "start_time="
16+
SET "duration="
17+
SET "time_range="
18+
SET "palette="
19+
SET "max_colors="
20+
SET "dither="
21+
22+
SET input=%~1
23+
SET base_file=%~dpn1
24+
SET input_filename=%~n1%~x1
25+
SHIFT
26+
27+
IF "%input%" == "" (
28+
ECHO Video to GIF converter v1.0.0 ^(C^) 2017, Nabi KaramAliZadeh ^<nabikaz@gmail.com^>
29+
ECHO You can download it from here: https://github.com/NabiKAZ/video2gif
30+
ECHO This tools use of ffmpeg here: http://ffmpeg.zeranoe.com/builds
31+
ECHO.
32+
ECHO Usage: video2gif SOURCE_FILE [OPTIONS]
33+
ECHO.
34+
ECHO Options:
35+
ECHO SOURCE_FILE Source video file name for convert
36+
ECHO also you can drag and drop source video file to this batch file directly
37+
ECHO -o Output destination .GIF file name
38+
ECHO if not set it, use video source file name with new .gif extension
39+
ECHO -y Overwrite destination file
40+
ECHO if not set it, appears prompt for overwrite destination file
41+
ECHO -f Frame per second ^(fps^)
42+
ECHO default: 15
43+
ECHO -w Width scale of destination gif file
44+
ECHO in pixel unit and ^(-1^) for use original width, default: -1
45+
ECHO -s Start time of video source for crop
46+
ECHO in second or time format ^(ex. 1:12^), if set it, so must be set ^(-d^) param
47+
ECHO -d Duration time of video source for crop
48+
ECHO in second, if set it, so must be set ^(-s^) param
49+
ECHO -c Maximum number of colors to the palette
50+
ECHO must be ^(^<=256^), default: 256
51+
ECHO -q Quality of destination gif file
52+
ECHO must be a number between 1^(low^) to 6^(high^), default: 5
53+
ECHO.
54+
ECHO Examples:
55+
ECHO video2gif sample.mp4
56+
ECHO video2gif sample.mp4 -y -w 60 -q 1 -f 10
57+
ECHO video2gif sample.mp4 -o new_file.gif -y -w 100 -f 10 -s 10 -d 5
58+
ECHO video2gif sample.mp4 -s 0:30 -d 20 -c 128 -q 6
59+
ECHO video2gif sample.mp4 -s 1:16.5 -d 8.3
60+
GOTO :EOF
61+
)
62+
63+
:loop
64+
IF NOT "%~1"=="" (
65+
IF "%~1"=="-o" SET "output=%~2" & SHIFT
66+
IF "%~1"=="-y" SET "overwrite=-y"
67+
IF "%~1"=="-f" SET "fps=%~2" & SHIFT
68+
IF "%~1"=="-w" SET "width=%~2" & SHIFT
69+
IF "%~1"=="-s" SET "start_time=%~2" & SHIFT
70+
IF "%~1"=="-d" SET "duration=%~2" & SHIFT
71+
IF "%~1"=="-c" SET "max_colors=%~2" & SHIFT
72+
IF "%~1"=="-q" SET "dither=%~2" & SHIFT
73+
SHIFT
74+
GOTO :loop
75+
)
76+
77+
if "%output%"=="" SET output=%base_file%.gif
78+
if "%fps%"=="" SET fps=15
79+
if "%width%"=="" SET width=-1
80+
if not "%start_time%"=="" if not "%duration%"=="" SET time_range=-ss %start_time% -t %duration%
81+
if "%max_colors%"=="" SET max_colors=256
82+
if "%dither%"=="" SET dither=sierra2_4a
83+
if "%dither%"=="1" SET dither=none
84+
if "%dither%"=="2" SET dither=sierra2
85+
if "%dither%"=="3" SET dither=floyd_steinberg
86+
if "%dither%"=="4" SET dither=heckbert
87+
if "%dither%"=="5" SET dither=sierra2_4a
88+
if "%dither%"=="6" SET dither=bayer
89+
90+
SET palette=%TEMP%\%input_filename%.png
91+
SET filters=fps=%fps%,scale=%width%:-1:flags=lanczos
92+
93+
ECHO Generating palette file. Please wait...
94+
95+
CALL ffmpeg -v error %time_range% -i %input% -vf "%filters%,palettegen=max_colors=%max_colors%" -y %palette%
96+
97+
IF NOT EXIST "%palette%" (
98+
ECHO Failed to generate palette file.
99+
GOTO :EOF
100+
)
101+
102+
ECHO Generating gif file. Please wait...
103+
104+
CALL ffmpeg -v error %time_range% -i %input% -i %palette% -lavfi "%filters% [x]; [x][1:v] paletteuse=dither=%dither%" %overwrite% %output%
105+
106+
IF NOT EXIST "%output%" (
107+
ECHO Failed to generate gif file.
108+
GOTO :EOF
109+
)
110+
111+
DEL /F /Q %palette%
112+
113+
ECHO "%output%" Gif file generated successfully.
114+
ECHO Done.

0 commit comments

Comments
 (0)