Advertisement
Guest User

Untitled

a guest
May 13th, 2011
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. //This file depends on ffmpeg.
  2. //Linux users: apt-get install ffmpeg
  3. //Windows users: see http://ffmpeg.zeranoe.com/builds/
  4. //Put the first argument (file path) in "quotes" if it has spaces or the program will return an exception
  5. import java.io.*;
  6. import java.util.Scanner;
  7.  
  8. public class ffmpeg{
  9.     public static String _videopath = "../recording.avi";
  10.     public static String _r = "1"; //_r is the number of frames per second that ffmpeg should grab
  11.     public static String ffmpegpath = "C:\fmpeg\bin\ffmpeg.exe";
  12.     static StringBuffer command = new StringBuffer();
  13.     static Scanner input = new Scanner(System.in);
  14.         public static void main(String args[]){
  15.            
  16.             //this segment of code identifies the operating system and generates the ffmpeg path
  17.             if(System.getProperty("os.name").startsWith("Windows")){
  18.                 System.out.println("Please enter the path to ffmpeg on your Windows system: ");
  19.                 ffmpegpath = input.nextLine();
  20.                 try{
  21.                     File testffmpeg = new File(ffmpegpath + "\\ffmpeg.exe");
  22.                     if(!testffmpeg.exists()){
  23.                         throw new Exception("ffmpeg could not be found at the specified path.");
  24.                     }
  25.                 }catch(Exception ex){
  26.                     System.err.print(ex.getMessage());
  27.                 }
  28.             }else{
  29.                 ffmpegpath = "ffmpeg";
  30.             }
  31.            
  32.             //check the command-line arguments for any commands
  33.             switch(args.length){
  34.                 case 1:
  35.                     _videopath=args[0];
  36.                     break;
  37.                 case 2:
  38.                     _videopath=args[0];
  39.                     _r=args[1];
  40.                     break;
  41.                 default:
  42.             }
  43.            
  44.             //this segment of code checks if the file exists and gives us the directory it's in if it does
  45.             try{
  46.                 File testVideo = new File(_videopath);
  47.                 if(!testVideo.exists()){
  48.                     throw new Exception("File '" + _videopath + "' does not exist.");
  49.                 }
  50.             }catch(Exception ex){
  51.                 System.err.print(ex.getMessage());
  52.             }
  53.            
  54.             //setup originalframes-recording directory, where recording is the video name minus its extension
  55.             File testVideo = new File(_videopath);
  56.             File _rootpath = new File(testVideo.getParent());
  57.             String _videoname = testVideo.getName();
  58.             _videoname = _videoname.substring(0, _videoname.lastIndexOf("."));
  59.             File _originalframespath = new File(_rootpath.getAbsolutePath()+"/originalframes-"+_videoname);
  60.             try{
  61.                 if(_rootpath.exists()){
  62.                     if(!_originalframespath.exists())
  63.                         _originalframespath.mkdir();
  64.                 }else
  65.                     throw new Exception("Directory '" + _rootpath.getAbsolutePath() + "' does not exist.");
  66.             }catch(Exception ex){
  67.                 System.err.print(ex.getMessage());
  68.             }
  69.            
  70.             //build ffmpeg command
  71.             String commandStr[] = {ffmpegpath,"-i",_videopath,"-r", _r,"-f","image2",_originalframespath.getAbsolutePath()+"/%03d.jpg"};
  72.        
  73.             //print the command to execute
  74.             for(String x:commandStr){
  75.                 System.out.print(x+" ");
  76.             }
  77.            
  78.             //execute ffmpeg with the command generated by the previous segment
  79.             try{
  80.                 Process ffmpeg = Runtime.getRuntime().exec(commandStr);//THIS DOES NOT WORK
  81.                 BufferedReader in = new BufferedReader(new InputStreamReader(ffmpeg.getInputStream()));
  82.                 String line = null;
  83.                 while((line = in.readLine())!= null){
  84.                     System.out.println(line);  
  85.                 }
  86.             }catch(IOException ex){
  87.                 ex.printStackTrace();
  88.             }
  89.         }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement