Linux视频,图片 缩略图处理(FFMPEG,Imagescaling)

终端传送的多媒体有些需要生成缩略图以给用户更好更快速的浏览体验。

当然缩略图可以上传时生成并保存在数据库,也可以不保存并在在使用时动态生成,各有优劣。

下面是本人在Mongo+Play!+Scala中的应用

Imagescaling

包:java-image-scaling-0.8.6.jar

import org.apache.commons.io.output.ByteArrayOutputStream;
import play.Logger;
import com.mortennobel.imagescaling.DimensionConstrain;
import com.mortennobel.imagescaling.ResampleOp;


public class ImageUtil {


    public static ByteArrayOutputStream scale(BufferedImage source, int width,
            int length, String type) throws FileNotFoundException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ResampleOp resampleOp = new ResampleOp(
                    DimensionConstrain.createMaxDimension(width, length, true));
            BufferedImage rescaled = resampleOp.filter(source, null);
            ImageIO.write(rescaled, type, baos);
        } catch (Exception e) {
            Logger.info("Create Image thumbnail fail! " + e);
        }
        return baos;
    }
}

FFMPEG:

包:ffmpeg-2.1.tar.bz2


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class VideoUtil {

    public static boolean transfer(String inFile, String outFile, int length,
            int width) {
        String command = "ffmpeg -i " + inFile
                + " -y -f image2 -ss 00:00:01 -t 00:00:01 -s " + length + "x"
                + width + " " + outFile;
        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(command);
            java.io.InputStream stderr = proc.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null)
                System.out.println(line);
        } catch (Throwable t) {
            return false;
        }
        return true;
    }
}


FFMPEG需要环境搭建,由于Linux环境搭建中细节网上不多,这里简单罗列:

1,使用apt-get获得最新SVN

2,使用SVN获得最新FFMPEG,地址:svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg

3,/configure --prefix=/usr/local/ffmpeg --enable-cross-compile --enable-shared
      --enable-shared 是允许其编译产生动态库,在以后的编程中要用到这个几个动态库。--prefixFFMPEG安装目录

4,使用make,make install命令编译并安装

5,安装之后在/usr/local/ffmpeg会看到有三个目录

lib 动态链接库位置

include 编程要用到头文件

bin 执行文件所在的目录

我们可以把lib中的三个链接库libavcodec.so libavformat.so libavutil.so复制到/usr/lib下。

把include目录下的ffmpeg目录复制到/usr/include下。

执行bin目录下的ffplay,可以去播放音频或者视频文件。

例如播放1.mp3 命令为./ffplay 1.mp3

bin目录下还有两个文件:ffmpeg和ffserver

ffmpeg是一个很好的视频和音频的格式转化工具

如果不想生成ffserver,只要在./configure的时候加--disable-ffserver即可。

猜你喜欢

转载自ariesx.iteye.com/blog/1969810
今日推荐