FFMPEG 功能很强大,做视频必备的软件。大家可通过 http://ffmpeg.org/ 了解。Windows版本的软件,可通过 http://ffmpeg.zeranoe.com/builds/ 下载。

因为有这个需求,通过 ffmpeg 获取视频第一帧图片

Google一把,就有了结果。

参考:

1、http://www.codereye.com/2010/05/get-first-and-last-thumb-of-video-using.html

1、VideoInfo.java 获取视频信息。读者可认真研究此方法。

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* 获取视频的信息
* FFMPEG homepage http://ffmpeg.org/about.html
*/
public class VideoInfo
{
//视频路径
private String ffmpegApp;
//视频时
private int hours;
//视频分
private int minutes;
//视频秒
private float seconds;
//视频width
private int width;
//视频height
private int heigt; public VideoInfo() {} public VideoInfo(String ffmpegApp)
{
this.ffmpegApp = ffmpegApp;
} public String toString()
{
return "time: " + hours + ":" + minutes + ":" + seconds + ", width = " + width + ", height= " + heigt;
} public void getInfo(String videoFilename) throws IOException,
InterruptedException
{
String tmpFile = videoFilename + ".tmp.png";
ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp, "-y",
"-i", videoFilename, "-vframes", "1", "-ss", "0:0:0", "-an",
"-vcodec", "png", "-f", "rawvideo", "-s", "100*100", tmpFile); Process process = processBuilder.start(); InputStream stderr = process.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line;
//打印 sb,获取更多信息。 如 bitrate、width、heigt
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null)
{
sb.append(line);
} new File(tmpFile).delete(); System.out.println("video info:\n" + sb);
Pattern pattern = Pattern.compile("Duration: (.*?),");
Matcher matcher = pattern.matcher(sb); if (matcher.find())
{
String time = matcher.group(1);
calcTime(time);
} pattern = Pattern.compile("w:\\d+ h:\\d+");
matcher = pattern.matcher(sb); if (matcher.find())
{
String wh = matcher.group();
//w:100 h:100
String[] strs = wh.split("\\s+");
if(strs != null && strs.length == 2)
{
width = Integer.parseInt(strs[0].split(":")[1]);
heigt = Integer.parseInt(strs[1].split(":")[1]);
}
} process.waitFor();
if(br != null)
br.close();
if(isr != null)
isr.close();
if(stderr != null)
stderr.close();
} private void calcTime(String timeStr)
{
String[] parts = timeStr.split(":");
hours = Integer.parseInt(parts[0]);
minutes = Integer.parseInt(parts[1]);
seconds = Float.parseFloat(parts[2]);
} public String getFfmpegApp()
{
return ffmpegApp;
} public void setFfmpegApp(String ffmpegApp)
{
this.ffmpegApp = ffmpegApp;
} public int getHours()
{
return hours;
} public void setHours(int hours)
{
this.hours = hours;
} public int getMinutes()
{
return minutes;
} public void setMinutes(int minutes)
{
this.minutes = minutes;
} public float getSeconds()
{
return seconds;
} public void setSeconds(float seconds)
{
this.seconds = seconds;
} public int getWidth()
{
return width;
} public void setWidth(int width)
{
this.width = width;
} public int getHeigt()
{
return heigt;
} public void setHeigt(int heigt)
{
this.heigt = heigt;
} public static void main(String[] args)
{
VideoInfo videoInfo = new VideoInfo("D:\\ffmpeg\\bin\\ffmpeg.exe");
try
{
videoInfo.getInfo("f:/reyo/test.mkv");
System.out.println(videoInfo);
} catch (Exception e)
{
e.printStackTrace();
}
}
}

从此类,可以看出,我把 ffmpeg,解压在了 D:\\ffmpeg\\bin\\ffmpeg.exe 路径下,传入一个视频地址,就可以得出视频信息。

2、VideoThumbTaker.java 获取视频指定播放时间的图片

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader; /**
* @author reyo
* FFMPEG homepage http://ffmpeg.org/about.html
* By Google Get first and last thumb of a video using Java and FFMpeg
* From http://www.codereye.com/2010/05/get-first-and-last-thumb-of-video-using.html
*/ public class VideoThumbTaker
{
protected String ffmpegApp; public VideoThumbTaker(String ffmpegApp)
{
this.ffmpegApp = ffmpegApp;
} @SuppressWarnings("unused")
/****
* 获取指定时间内的图片
* @param videoFilename:视频路径
* @param thumbFilename:图片保存路径
* @param width:图片长
* @param height:图片宽
* @param hour:指定时
* @param min:指定分
* @param sec:指定秒
* @throws IOException
* @throws InterruptedException
*/
public void getThumb(String videoFilename, String thumbFilename, int width,
int height, int hour, int min, float sec) throws IOException,
InterruptedException
{
ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp, "-y",
"-i", videoFilename, "-vframes", "1", "-ss", hour + ":" + min
+ ":" + sec, "-f", "mjpeg", "-s", width + "*" + height,
"-an", thumbFilename); Process process = processBuilder.start(); InputStream stderr = process.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null)
;
process.waitFor(); if(br != null)
br.close();
if(isr != null)
isr.close();
if(stderr != null)
stderr.close();
} public static void main(String[] args)
{
VideoThumbTaker videoThumbTaker = new VideoThumbTaker("D:\\ffmpeg\\bin\\ffmpeg.exe");
try
{
videoThumbTaker.getThumb("f:/reyo/test.mkv", "C:\\thumbTest.png", 800, 600, 0, 0, 9);
System.out.println("over");
} catch (Exception e)
{
e.printStackTrace();
}
}
}

3、VideoFirstThumbTaker.java 获取第一帧图片

import java.io.IOException;

/***
*
* 得到第一秒(也是第一帧)图片
*/
public class VideoFirstThumbTaker extends VideoThumbTaker
{
public VideoFirstThumbTaker(String ffmpegApp)
{
super(ffmpegApp);
} public void getThumb(String videoFilename, String thumbFilename, int width,
int height) throws IOException, InterruptedException
{
super.getThumb(videoFilename, thumbFilename, width, height, 0, 0, 1);
}
}

4、VideoLastThumbTaker.java 获取最后一帧图片

/**
* 得到最后一秒(也是最后一帧)图片
*/ public class VideoLastThumbTaker extends VideoThumbTaker
{
public VideoLastThumbTaker(String ffmpegApp)
{
super(ffmpegApp);
} public void getThumb(String videoFilename, String thumbFilename, int width,
int height) throws IOException, InterruptedException
{
VideoInfo videoInfo = new VideoInfo(ffmpegApp);
videoInfo.getInfo(videoFilename);
super.getThumb(videoFilename, thumbFilename, width, height,
videoInfo.getHours(), videoInfo.getMinutes(),
videoInfo.getSeconds() - 0.2f);
}
}

java获取视频播第一帧的更多相关文章

  1. java获取视频的第一帧

    //------------maven配置文件--------------- <dependency> <groupId>org.bytedeco</groupId> ...

  2. 第五十二篇、 OC获取视频的第一帧图片thumbnailImage

    获取视频的第一帧图片 有时候我们拍摄完视频后,希望获取一张图片当作这个视频的介绍,这个图片thumbnailImage可以从视频的第一帧获取到. 我们的思路是先获取视频的URL,然后初始化一个MPMo ...

  3. PHP获取视频的第一帧与时长

    //获得视频文件的缩略图 function getVideoCover($file,$time,$name) { if(empty($time))$time = '1';//默认截取第一秒第一帧 $s ...

  4. 在Android中如何获取视频的第一帧图片并显示在一个ImageView中

    String path  = Environment.getExternalStorageDirectory().getPath(); MediaMetadataRetriever media = n ...

  5. C#:获取视频某一帧的缩略图

    读取方式:使用ffmpeg读取,所以需要先下载ffmpeg.网上资源有很多. 原理是通过ffmpeg执行一条命令获取视频某一帧的缩略图. 首先,需要获取视频的帧高度和帧宽度,这样获取的缩略图才不会变形 ...

  6. java获取当月的第一天和最后一天,获取本周的第一天和最后一天

    /** * 获取指定日期所在周的第一天和最后一天,用下划线连接 * @param dataStr * @return * @throws ParseException */ public static ...

  7. 关于ffmpeg /iis 8.5 服务器下,视频截取第一帧参数配置

    ffmpeg 视频截取第一帧参数配置: 网站找了很多资料,但是都不能满足要求,然后自己写下解决过程. 首先看自己PHP 版本,安全选项里面 php5.4  跟php5.6 是不一样的.去除里面的sys ...

  8. java获取当前月第一天和最后一天,上个月第一天和最后一天

    package com.test.packager; import java.text.ParseException; import java.text.SimpleDateFormat; impor ...

  9. java获取视频缩略图

    近期由于在做一个关于视频播放的项目,需要使用程序自动获取视频文件的缩略图,特写此文供其他人参考,有不清楚之楚可以给我留言. 1.使用工具:ffmpeg, 官网下载地址:http://ffmpeg.or ...

随机推荐

  1. java 轨迹栈

    printStackTrace()方法所提供的信息可以通过getStackTrace()方法直接访问. getStackTrace()方法返回一个由根轨迹中的元素所构成的数组,每一个元素都表示栈中的一 ...

  2. bnu 10809 聚餐

    Lolilu大牛又要请客了~~有些同学呢,是果断要去的,而有些同学呢,只有确定心中的大牛会参加,他才会参加.Lolilu决定请大家去吃金钱豹,因此希望你告诉他一共会有多少人参加,他才知道带多少钱比较合 ...

  3. JS的异步模式

    JS的异步模式:1.回调函数:2.事件监听:3.观察者模式:4.promise对象 JavaScript语言将任务的执行模式可以分成两种:同步(Synchronous)和异步(Asychronous) ...

  4. PHP实现数据分页显示

    分页在后台管理中是经常使用的功能,分页显示方便大量数据的管理. 实例代码如下: <!DOCTYPE html> <html> <head> <meta cha ...

  5. 新手:Qt之QLabel类的应用

    在Qt中,我们不可避免的会用到QLabel类.而Qlabel的强大功能作为程序员的你有多少了解? 下面,跟着我一起在来学习一下吧! 1.添加文本 Qlabel类添加文本有两种方式,一种是直接在实现时添 ...

  6. 转载-解决ORACLE 在控制台进行exp,导出时,空表不能导出

    一.问题原因: 11G中有个新特性,当表无数据时,不分配segment,以节省空间 1.insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除,则产生segm ...

  7. Python - 计算个人所得税

    最近在学python,写了个计算个人所得税计算的脚本,分享. 以下为python3适用版本 #!/usr/bin/python # -*- coding: UTF-8 -*- # 该python脚本用 ...

  8. 使用Plant Simulation连接SQL Server

    1. 在管理类库中添加ODBC. 2. 在控制面板->管理工具中设置ODBC,添加SQL Server服务. 3. 在plant simulation中将信息流中的ODBC添加到Frame中. ...

  9. Java 操纵XML之修改XML文件

    Java 操纵XML之修改XML文件 一.JAVA DOM PARSER DOM interfaces The DOM defines several Java interfaces. Here ar ...

  10. 【NOI2005】聪聪和可可 概率与期望 记忆化搜索

    1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1635  Solved: 958[Submit][Statu ...