0

i try to convert video using ffmpeg in ubuntu.

ffmpeg -i inputfile.flv -sameq outputfile.mpeg

this works if change directory to inputfile directory.

is that posible to use this command ?

ffmpeg -i "home/Documents/inputfile.flv" -sameq "home/Documents/outputfile.mpeg"

i don't want to change directory when i use that command, because that command is using for my java code. so my input file and output file is variable in my code .

here's my full code

package Converter;

import Controller.ConvertedButtonListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;

/**
 *
 * @author roylisto
 */
public class VideoConverter {
    private String defaultFile;
    private String convertedFile;   
    private ConverterThread myThread;
    private ConvertedButtonListener butListener;
    public VideoConverter(String fileDir,String convertOutput,ConvertedButtonListener buttonListener){
        this.defaultFile=fileDir;
        this.convertedFile=convertOutput;
        this.butListener=buttonListener;        
    }
    public void convertToMjpeg(){                             
        String[] listCommands={"ffmpeg","-i","\""+defaultFile+"\"","-qscale","0","\""+convertedFile+"\""};
        myThread=new ConverterThread(listCommands,this);
        myThread.start();                
    }    
    public void setCommandStream(String stream){
        butListener.setCommandOutput(stream);
    }
    class ConverterThread extends Thread{
        VideoConverter vc;
        String[] command;
        ConverterThread(String[] command,VideoConverter vc){        
            this.command=command;            
            this.vc=vc;
        }        
        public void run(){      
            synchronized(vc){
                try{          
                    String s = null;
                    Process process = new ProcessBuilder(command).start();                
                    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                    StringBuffer start= new StringBuffer();
                    // read the output from the command                    
                        while ((s = stdInput.readLine()) != null)
                        {
                            start.append(s);
                            vc.setCommandStream(s);
                        }
                        stdInput.close();
                        // read any errors from the attempted command                
                        while ((s = stdError.readLine()) != null)
                        {
                            start.append(s);    
                            vc.setCommandStream(s);
                        }
                }catch(Exception ex){
                    System.out.println(ex.toString());
                } 
            }
        }
    }
}

until now my code works well in windows with some modification like change ffmpeg to ffmpeg.exe because ffmpeg isn't native in my windows. but when i use my code in ubuntu it show this error

"/home/roylisto/Documents/Tugas Akhir/Video Master/3a.avi": No such file or directory

UPDATE solve problem, here's my code :)

package Converter;

import Controller.ConvertedButtonListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;

/**
 *
 * @author roylisto
 */
public class VideoConverter {
    private String defaultFile;
    private String convertedFile;   
    private ConverterThread myThread;
    private ConvertedButtonListener butListener;
    public VideoConverter(String fileDir,String convertOutput,ConvertedButtonListener buttonListener){
        this.defaultFile=fileDir;
        this.convertedFile=convertOutput;
        this.butListener=buttonListener;        
    }
    public void convertToMjpeg(){                             
        String[] listCommands={"ffmpeg","-i",defaultFile,"-qscale","0",convertedFile};
        myThread=new ConverterThread(listCommands,this);
        myThread.start();                
    }    
    public void setCommandStream(String stream){
        butListener.setCommandOutput(stream);
    }
    class ConverterThread extends Thread{
        VideoConverter vc;
        String[] command;
        ConverterThread(String[] command,VideoConverter vc){        
            this.command=command;            
            this.vc=vc;
        }        
        public void run(){      
            synchronized(vc){
                try{          
                    String s = null;
                    Process process = new ProcessBuilder(command).start();                
                    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                    StringBuffer start= new StringBuffer();
                    // read the output from the command                    
                        while ((s = stdInput.readLine()) != null)
                        {
                            start.append(s);
                            vc.setCommandStream(s);
                        }
                        stdInput.close();
                        // read any errors from the attempted command                
                        while ((s = stdError.readLine()) != null)
                        {
                            start.append(s);    
                            vc.setCommandStream(s);
                        }
                }catch(Exception ex){
                    System.out.println(ex.toString());
                } 
            }
        }
    }
}
Roy
  • 398
  • 1
  • 6
  • 20

1 Answers1

0

is that posible to use this command ?

Yes, it is possible, as long as you specify a valid absolute or a relative path:

"home/Documents/inputfile.flv"

Should be:

"/home/Documents/inputfile.flv"

Otherwise it'd look for a home directory inside the current working directory. Notice the / at the beginning.

And as for this:

"/home/roylisto/Documents/Tugas Akhir/Video Master/3a.avi": No such file or directory

Are you really shure the file is there and you have r/w access to that directory?

arielnmz
  • 8,354
  • 9
  • 38
  • 66
  • what is the output of the: `ls -l "/home/roylisto/Documents/Tugas Akhir/Video Master/3a.avi"` command? – arielnmz Jun 02 '14 at 20:39
  • here's the output -rw-r--r-- 1 roylisto roylisto 29389724 Des 7 2009 /home/roylisto/Documents/Tugas Akhir/Video Master/3a.avi – Roy Jun 02 '14 at 20:42
  • It's strange, have you tried moving the file to another (simplier) path? like the home directory? (or at least one without spaces in its name?) – arielnmz Jun 02 '14 at 21:05
  • still same error "/home/roylisto/3a.avi": No such file or directory , i dont have a clue too, is there something wrong with my code? – Roy Jun 02 '14 at 21:36
  • As far as I can tell your code is alright. Can you execute `ls -s` from inside your code (instead of ffmpeg)? – arielnmz Jun 02 '14 at 21:39
  • this code is for windows String[] listCommands={"ffmpeg","-i","\""+defaultFile+"\"","-qscale","0","\""+convertedFile+"\""}; – Roy Jun 02 '14 at 21:40
  • and when i change to this String[] listCommands={"ffmpeg","-i",""+defaultFile+"","-qscale","0",""+convertedFile+""}; – Roy Jun 02 '14 at 21:40
  • works really well :D, thanks dude, i can't find it without ur help :D – Roy Jun 02 '14 at 21:41
  • Not really much my help, but I'm glad you solved it. Also, have you tried to use `/` instead of "\" and put it on both linux and win versions? – arielnmz Jun 02 '14 at 21:44
  • or like this String[] listCommands={"ffmpeg","-i",defaultFile,"-qscale","0",convertedFile}; ,hahaha – Roy Jun 02 '14 at 21:44
  • this is why i use "\" :) http://stackoverflow.com/questions/8354168/quotation-marks-inside-a-string – Roy Jun 02 '14 at 21:49
  • Oh, right. I thought you were using that as the root dir for the file. My bad. – arielnmz Jun 02 '14 at 21:56