简体   繁体   中英

fluent-ffmpeg h264 to gif throwing "error 1"

below is my code to convert h264 to gif

var ffmpeg = require("fluent-ffmpeg");
var inFilename = "/home/pi/Videos/video.mp4";
var outFilename = "/home/pi/Videos/video.gif";
var fs = require('fs');
ffmpeg(inFilename)
  .outputOptions("-c:v", "copy")
  .output(outFilename)
  .run();

this code works PERFECTLY when it goes from h264 to mp4 just wondering why it doesnt work with h264 to gif or if I could make it work.

The main problem is that in this case you cannot use H.264 inside a GIF file so you have to remove the outputOptions line (which tries to copy the H.264 video directly to GIF) in order for FFmpeg to reencode the input video.

However, converting video size and frame rate AS-IS to GIF animation is not always a wise thing to do, so I would recommend adding a new option (see for example this answer for more examples and options) to account for that.

Lets modify the code a bit as a start; lets replace the output options for this case:

var ffmpeg = require("fluent-ffmpeg");
var inFilename = "/home/pi/Videos/video.mp4";
var outFilename = "/home/pi/Videos/video.gif";

ffmpeg(inFilename)
  .outputOption("-vf", "scale=320:-1:flags=lanczos,fps=15")
  .save(outFilename);

The options here are the same as running FFmpeg directly with:

ffmpeg -i inputfile.h264 -vf scale=320:-1:flags=lanczos,fps=15 outputfile.gif

The parameters are:

  • scale=320:-1 will scale to width 320 pixels. -1 will use a height proportional to width. You can flip them around to use height as absolute size.
  • flags=lanczos is the algorithm used to resample the image. lanczos provides good resample quality
  • fps=15 means the GIF will run at about 15 frames per second (FPS).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM