ffmpeg-gif.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python
  2. import sys
  3. import argparse
  4. import os
  5. def parse_arguments():
  6. parser = argparse.ArgumentParser(description='ffmpeg gif making script.')
  7. parser.add_argument('-s', '--start', type=str, metavar='00:00:00', help='Start time of the clip')
  8. parser.add_argument('-e', '--end', type=str, metavar='00:00:05', help='End time of the clip')
  9. parser.add_argument('-i', '--input', type=str, metavar='FILE', help='Input file')
  10. parser.add_argument('-o', '--output', type=str, metavar='FILE', help='Output file')
  11. parser.add_argument('-t', '--text', type=str, metavar='TEXT', help='Output file')
  12. return parser.parse_args()
  13. def main():
  14. args = parse_arguments()
  15. if not os.path.isfile(args.output):
  16. print("File not found!")
  17. sys.exit(1)
  18. print("---------------")
  19. os.system(f'ffmpeg -ss {args.start} -t {args.end} -i "{args.input}" -filter_complex "[0:v] palettegen" palette.png')
  20. print("---------------")
  21. os.system(f'ffmpeg -ss {args.start} -t {args.end} -i "{args.input}" -i palette.png -filter_complex "[0:v] fps=10,scale=720:-1 [new];[new][1:v] paletteuse" ffmpeg-meme-gif-file.gif')
  22. print("---------------")
  23. if not args.text == "":
  24. os.system(f'ffmpeg -i ffmpeg-meme-gif-file.gif -vf "drawtext=fontsize=45:fontcolor=#ffffff:bordercolor=black:borderw=2:x=(w-text_w)/2:y=(h-text_h)/2:text=\'{args.text}\'" -codec:a copy {args.output}')
  25. os.remove("palette.png")
  26. os.remove("ffmpeg-meme-gif-file.gif")
  27. if __name__ == "__main__":
  28. main()