在Linux上将视频转换成动态gif图片 (附:ffmpeg和ImageMagick安装方法)

虽然曾经被认为是过时的艺术形式,但动态GIF图片现在复苏了。如果你还没有留意到,不少在线分享和社交网络网站都开始支持动态GIF图片,例如,TumblrFlickrGoogle+Facebook的部分地方。由于在消费和共享上的容易,GIF的动画已经成为主流互联网文化的一部分了。

所以,你们中的一些人会好奇怎样才能生成这样的动态GIF图片。已经有各种各样专门用来生成动态GIF图片的在线或离线工具。另一种选择是创建一副动态GIF图片时关闭现有的视频剪辑。在这个教程中,我会描述在Linux上如何将一段视频文件转换成一副动态GIF图片

作为一个更有用的例子,让我展示如何将一个YouTube视频转换成一副动态GIF图片

 

第一步:下载YouTube视频

首先,下载一个你想要转换的YouTube视频。你可以使用youtube-dl这个工具将YouTube视频保存为MP4文件。假设你把你最爱的YouTube视频保存为"funny.mp4"。(译注:对于墙内的同学,请无视YT吧,自行去好人楼主那里寻找一个MP4吧,;-})

 

第二步:从视频中解压视频帧

接下来,在Linux系统上安装FFmpeg,我会用这个工具去解压从视频中解压出视频帧。

下面的指令会解压出独立的视频帧,将它们保存为GIF图片。确保使用诸如("out%04d.gif")的输出文件格式。这样,独立的帧就被合适地命名并保存。

ffmpeg -t <时长> -ss <hh:mm:ss格式的开始位置> -i <视频文件> out%04d.gif

例如,如果你想解压输入视频的视频帧,从第10秒开始,每5秒一帧,请运行下列命令。

$ ffmpeg -t 5 -ss 00:00:10 -i funny.mp4 out%04d.gif

在完成FFmpeg之后,你会看到一组创建出来的GIF文件,它们被命名为"out[\d+].gif"。

 

第三步:合并视频帧进一副动态GIF

下面这一步要合并单个的GIF文件成一副动态GIF图片。为此,你可以使用ImageMagick。

首先,如果你还没有的话,在Linux系统上安装ImageMagick

convert -delay <帧数>x<每秒帧数> -loop 0 out*gif <输出文件>

在这个命令中,"-delay"是控制动态速度的选项。这个选项表示在显示下一帧画面前需要等待的秒数:帧数/每秒帧数 。"-loop 0"选项表示动画的无限次循环。如果你愿意,你可以指定"-loop N"让动画只重复N次。

例如,为了生成一副每秒20帧和循环无数次的动态GIF图片,使用如下命令。

$ convert -delay 1x20 -loop 0 out*.gif animation.gif

 

第四步(可选):减少动态GIF的大小

最后这一步(可选)是通过使用ImageMagick的GIF优化功能来减少生成的GIF文件的大小。

使用下列命令去减少GIF大小。

convert -layers Optimize animation.gif animation_small.gif

现在你已经准备好在你的社交网络上分享制作完成的GIF图片。下面是一副我从一个可爱的YouTube视频中生成的GIF样例图片。

享受技术带来的乐趣吧!:-)

在Linux上将视频转换成动态gif图片 (附:ffmpeg和ImageMagick安装方法)


via: http://xmodulo.com/2013/11/convert-video-animated-gif-image-linux.html

译者:KayGuoWhu 校对:wxy

原文由 LCTT 原创翻译,Linux中国 荣誉推出

原文地址:http://linux.cn/thread/12298/1/1/

 

1. 安装ffmpeg

Install FFmpeg in Ubuntu

Either click this link or run this command in the terminal to install FFmpeg.

sudo apt-get install ffmpeg

 

Install FFmpeg in Fedora

FFmpeg can be directly installed from the repos. Just run the following command.

su -c 'yum install ffmpeg'

 

Install FFmpeg in any Linux system from source

This howto will be helpful to all the linuxers who either want to have a fully customized installation of FFmpeg or are having some trouble in installing the default package from the distro repos or want to try the latest release.

First of all you will have to download the latest source from the main website. Now, untar it.

tar -xvjf ffmpeg-0.5.tar.bz2

 

Install FFmpeg with default options

once you are done with this, in order to install FFmpeg with the default config and options run

./configure

from within the FFmpeg source directory. When the configuration script finishes compile it by running make

make

If the compile finishes without any errors run 'make install' as root to install FFmpeg

su -c 'make install'

 

Install FFmpeg with customized settings

If you want to install FFmpeg by customizing some installation options then you can pass some special parameter while running configure script. Run the following command to find out various options available while running configure script.

./configure --help

This will show you various options to customize the default installation. Most of the time the default installation will work for you but there is one option which most of us might need freqeuntly. It is

--enable-libmp3lame: This one is a must if you want to work with mp3. Encoding mp3 won't be possible without it.

Although in order to make this option work you will need lame installed. You will get the warning at the time of configuring it. This can be done by installing the library "libmp3lame". You can either install it directly from the repos.

sudo apt-get install libmp3lame0

or, you can install libmp3lame directly from the source from their website.

Once you are done with 'lame's' installation. this run configure again, if successful run 'make' and then 'make install ' as root.

原文:http://linuxers.org/tutorial/how-install-ffmpeg-linux

2. 安装ImageMagick

ImageMagick is a suite of command line utilities for image conversion and editing. As one of the most popular image editing software suite, ImageMagick can support almost all kinds of raster image types including GIF, JPEG, PDF, PNG, Postscript, SVG, and TIFF.

 

Install ImageMagick on Debian, Ubuntu or Linux Mint

ImageMagick is included in a base repository of Debian-based systems. So simply use apt-get command.

sudo apt-get install imagemagick

 

Install ImageMagick on Fedora, CentOS or RHEL

ImageMagick is also part of standard RedHat-based systems. Thus installation is straightforward with yum command.

sudo yum install ImageMagick

 

Install ImageMagick on OpenSUSE

Installation of ImageMagick is straightforward on OpenSUSE with the use of zypper command.

sudo zypper install imagemagick

原文:http://xmodulo.com/2013/03/how-to-save-youtube-videos-on-linux.html

相关推荐