Java convert video to thumbnail--ffmpeg

Source: Internet
Author: User

Java generates video thumbnails

FFmpeg is used to generate thumbnails for uploading videos.

On the Internet to find how to compile FFmpeg method

But it feels so complicated.

I found the compiled ffmpeg file on the csdn.



Experience the FFmpeg is very powerful, most popular video formats can generate thumbnails

Start now

First download FFmpeg decompression

Method One:

Create a BAT file

Start

E:/ffmpeg/bin/ffmpeg.exe-i%1-ss 20-vframes 1-r 1-ac 1-ab 2-s 160*120-f image2%2

Exit

Note the meaning of using the red bar mark

E:/ffmpeg/bin/ffmpeg.exe FFmpeg's Path

%1%2 and C languages are a bit similar to reserved locations for parameters

20 How many seconds to intercept the picture

Open MyEclipse, build a project, and a Java file

Package test;

Import java.io.IOException;

public class Ffmpeg {
public static void Main (string[] args) {
Video files
String Videorealpath = "f://ffmpeg//wildlife.wmv";
The path (output path)
String Imagerealpath = "F://ffmpeg//a.jpg";
String basepath = "F://ffmpeg";
try {
Calling a batch file
Runtime.getruntime (). EXEC (
"cmd/c Start" + basepath + "//ffmpeg.bat" + basepath + "+ Videorealpath +" "
+ Imagerealpath);
} catch (IOException e) {
E.printstacktrace ();
}
}

}

Method Two:

Do not need to create a bat file

/**
* Convert video to thumbnail successfully returns TRUE, Failure returns false
* @param exepath executable Path
* @param videopath Video Path
* @param imagepath image path after conversion
* @return
*/
public static Boolean videotoimg (String exepath,string videopath,string ImagePath) {
File File = new file (Videopath);
if (!file.exists ()) {
SYSTEM.ERR.PRINTLN ("path [" + Videopath + "] Corresponding video file does not exist!");
return false;
}
list<string> commands = new java.util.arraylist<string> ();
Commands.add (ExePath);
Commands.add ("-i");
Commands.add (Videopath);
Commands.add ("-ss");
How many seconds to intercept the image (1 seconds after this interception of the picture)
Commands.add ("1");
Commands.add ("-vframes");
Commands.add ("1");
Commands.add ("-R");
Commands.add ("1");
Commands.add ("-ac");
Commands.add ("1");
Commands.add ("-ab");
Commands.add ("2");
Commands.add ("-S");
Commands.add ("320*240");
Commands.add ("-f");
Commands.add ("Image2");
Commands.add (ImagePath);
try {
Processbuilder builder = new Processbuilder ();
Builder.command (commands);
Builder.start ();
return true;
} catch (Exception e) {
E.printstacktrace ();
return false;
}
}

  public static void main(String[] args) throws Exception{          //1.获取当前工程目录,获取ffmpeg.exe          File directory = new File( "." );          String root = directory.getCanonicalPath();          String ffmpegPath = root + "\\ffmpeg\\ffmpeg.exe" ;                   //2.看不懂的童鞋们去读幼儿园吧(执行了后,刷新一下工程,注意观察你的工程目录中的image文件夹里,和控制台打印内容,成功了吧!)          String videoPath=root + "\\video\\demo.mp4" ;          String imagePath=root + "\\image\\ceshi.jpg" ;                   System.out.println( "执行结果" +getVideoImage(ffmpegPath,videoPath,imagePath));               }

/**       * 获得视频缩略图,获取成功返回true,获取失败返回false       * @param ffmpegPath  是ffmpeg.exe存放的路径       * @param path   是视频文件的存放路径       * @param outImagePath 输出缩略图的保存路径       * @return       */      public static boolean getVideoImage(String ffmpegPath,String path,String outImagePath) {          File file = new File(path);          if (!file.exists()) { //判断视频文件是否存在              System.err.println( "路径[" + path + "]对应的视频文件不存在!" );              return false ;          }          //设置参数          List<string> commands = new java.util.ArrayList<string>();          commands.add(ffmpegPath); //这里设置ffmpeg.exe存放的路径          commands.add( "-i" );          commands.add(path); //这里是设置要截取缩略图的视频的路径          commands.add( "-y" );          commands.add( "-f" );          commands.add( "image2" );          commands.add( "-ss" );          commands.add( "2" ); //这里设置的是要截取视频开始播放多少秒后的图,可以自己设置时间          commands.add( "-t" );          commands.add( "0.001" );          commands.add( "-s" );          commands.add( "320x240" ); //这里设置输出图片的大小          commands.add(outImagePath); //这里设置输出的的保存路径               try {              //截取缩略图并保存               ProcessBuilder builder = new ProcessBuilder();              builder.command(commands);              builder.start();              return true ;          } catch (Exception e) {              e.printStackTrace();              return false ;          }      } }

Java convert video to thumbnail--ffmpeg

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.