我有一张图片,我需要对生成的视频进行缩放效果。我几乎得到了想要的结果..但是。生成的图片看起来有点不稳定。这是因为裁剪和调整大小的四舍五入..所以每次转换时图片的中心都会稍微移动。我能用它做什么?或者也许还有其他方法可以实现它?在输入中我有 图片,zoom_type,zoom_percent,zoom_duration,scene_duration 这是完成这项工作的代码的一部分:
img = Magick::ImageList.new(picture).first
width, height = img.columns.to_f, img.rows.to_f
img_fps = 30
if width >= height
  aspect_ratio = (width / height)
  zoom_small_size = ((height * (100 - zoom_percent)) / 100).to_f
  small_size = height
else
  aspect_ratio = (height / width)
  zoom_small_size = ((width * (100 - zoom_percent)) / 100).to_f
  small_size = width
end
factor = (((small_size - zoom_small_size) / (img_fps * zoom_duration))).to_f
while factor < 2
  img_fps -= 1
  factor = ((small_size - zoom_small_size) / (img_fps * zoom_duration))
end
total_images = img_fps * scene_duration
zoom_images = img_fps * zoom_duration_seed
new_width =  width
new_height =  height
zoom_changed_small_size = small_size
total_images.times do |i|
if zoom_images > 0 && zoom_changed_small_size > zoom_small_size
  img_n = img.crop(new_width, new_height, true)
  new_width = (width <= height) ? (new_width - factor).round : (new_width-factor*aspect_ratio).round
  new_height = (width >= height) ? (new_height-factor).round : (new_height-factor*aspect_ratio).round
  zoom_changed_small_size = (width >= height) ? img_n.rows : img_n.columns
  img_n.resize_to_fill!(width, height)
  img_n.write("#{sprintf("img_%04d.jpg" % (i+1))}")
  zoom_images -= 1
  img = img_n.copy if zoom_images == 0 || zoom_changed_small_size <= zoom_small_size
  img_n.destroy!
else
  img.write("#{sprintf("img_%04d.jpg" % (i+1))}")
  puts "Writing - #{img.filename}"
end
end
然后 ffmpeg -y -f image2 -r 30 -i img_%04d.jpg -crf 0 -preset ultrafast -tune stillimage -pix_fmt yuv420p out.mp4