-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert2gif.py
53 lines (40 loc) · 1.31 KB
/
convert2gif.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import subprocess
import sys
from pathlib import Path
from typing import List
def convert(input_file: Path) -> None:
"""
Convert video file to animated gif
:param input_file: Video file to be converted
"""
# Check if input file exists
if not input_file.exists():
print(f"Input file {input_file.name} does not exist")
return
print(f"Converting {input_file.name} to animated gif...")
# Set up output file names
gif_path = input_file.with_suffix(".gif")
try:
# Call ffmpeg to convert video to gif
subprocess.call([
"ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-i",
input_file.__str__(),
"-vf", f"split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse",
"-loop",
"0",
gif_path.__str__()
])
print(f"Converted {input_file.name} to {gif_path} successfully.")
except Exception as e:
print(f"Error converting {input_file.name} to animated gif: {e}")
def main():
if len(sys.argv) < 2:
print("Please provide a video file to convert")
return
video = Path(sys.argv[1])
if not video.exists():
print(f"Video file {video.name} does not exist")
return
video = video.resolve()
convert(input_file=video)
main()