//This file depends on ffmpeg.
//Linux users: apt-get install ffmpeg
//Windows users: see http://ffmpeg.zeranoe.com/builds/
//Put the first argument (file path) in "quotes" if it has spaces or the program will return an exception
import java.io.*;
import java.util.Scanner;
public class ffmpeg{
public static String _videopath = "../recording.avi";
public static String _r = "1"; //_r is the number of frames per second that ffmpeg should grab
public static String ffmpegpath = "C:\fmpeg\bin\ffmpeg.exe";
static StringBuffer command = new StringBuffer();
static Scanner input = new Scanner(System.in);
public static void main(String args[]){
//this segment of code identifies the operating system and generates the ffmpeg path
if(System.getProperty("os.name").startsWith("Windows")){
System.out.println("Please enter the path to ffmpeg on your Windows system: ");
ffmpegpath = input.nextLine();
try{
File testffmpeg = new File(ffmpegpath + "\\ffmpeg.exe");
if(!testffmpeg.exists()){
throw new Exception("ffmpeg could not be found at the specified path.");
}
}catch(Exception ex){
System.err.print(ex.getMessage());
}
}else{
ffmpegpath = "ffmpeg";
}
//check the command-line arguments for any commands
switch(args.length){
case 1:
_videopath=args[0];
break;
case 2:
_videopath=args[0];
_r=args[1];
break;
default:
}
//this segment of code checks if the file exists and gives us the directory it's in if it does
try{
File testVideo = new File(_videopath);
if(!testVideo.exists()){
throw new Exception("File '" + _videopath + "' does not exist.");
}
}catch(Exception ex){
System.err.print(ex.getMessage());
}
//setup originalframes-recording directory, where recording is the video name minus its extension
File testVideo = new File(_videopath);
File _rootpath = new File(testVideo.getParent());
String _videoname = testVideo.getName();
_videoname = _videoname.substring(0, _videoname.lastIndexOf("."));
File _originalframespath = new File(_rootpath.getAbsolutePath()+"/originalframes-"+_videoname);
try{
if(_rootpath.exists()){
if(!_originalframespath.exists())
_originalframespath.mkdir();
}else
throw new Exception("Directory '" + _rootpath.getAbsolutePath() + "' does not exist.");
}catch(Exception ex){
System.err.print(ex.getMessage());
}
//build ffmpeg command
String commandStr[] = {ffmpegpath,"-i",_videopath,"-r", _r,"-f","image2",_originalframespath.getAbsolutePath()+"/%03d.jpg"};
//print the command to execute
for(String x:commandStr){
System.out.print(x+" ");
}
//execute ffmpeg with the command generated by the previous segment
try{
Process ffmpeg = Runtime.getRuntime().exec(commandStr);//THIS DOES NOT WORK
BufferedReader in = new BufferedReader(new InputStreamReader(ffmpeg.getInputStream()));
String line = null;
while((line = in.readLine())!= null){
System.out.println(line);
}
}catch(IOException ex){
ex.printStackTrace();
}
}
}