【问题标题】:use ffmpeg in java ubuntu在 java ubuntu 中使用 ffmpeg
【发布时间】:2014-06-02 20:28:13
【问题描述】:

我尝试在 ubuntu 中使用 ffmpeg 转换视频。

ffmpeg -i inputfile.flv -sameq outputfile.mpeg

如果将目录更改为输入文件目录,则此方法有效。

这个命令可以用吗?

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

我不想在使用该命令时更改目录,因为该命令用于我的 java 代码。 所以我的输入文件和输出文件在我的代码中是可变的。

这是我的完整代码

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());
                } 
            }
        }
    }
}

到目前为止,我的代码在 Windows 中运行良好,并进行了一些修改,例如将 ffmpeg 更改为 ffmpeg.exe,因为 ffmpeg 在我的 Windows 中不是本机的。但是当我在 ubuntu 中使用我的代码时 它显示此错误

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

更新 解决问题,这是我的代码:)

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());
                } 
            }
        }
    }
}

【问题讨论】:

标签: java ubuntu ffmpeg directory


【解决方案1】:

这个命令可以用吗?

可以,只要指定有效的绝对路径或相对路径即可:

"home/Documents/inputfile.flv"

应该是:

"/home/Documents/inputfile.flv"

否则它会在当前工作目录中寻找一个主目录。注意开头的/

至于这个:

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

您真的确定该文件在那里并且您对该目录具有 r/w 访问权限吗?

【讨论】:

  • ls -l "/home/roylisto/Documents/Tugas Akhir/Video Master/3a.avi" 命令的输出是什么?
  • 这里是输出 -rw-r--r-- 1 roylisto roylisto 29389724 Des 7 2009 /home/roylisto/Documents/Tugas Akhir/Video Master/3a.avi
  • 很奇怪,你有没有试过把文件移动到另一个(更简单的)路径?喜欢主目录? (或者至少一个名字中没有空格的?)
  • 还是一样的错误 "/home/roylisto/3a.avi": No such file or directory ,我也不知道,是不是我的代码有问题?
  • 据我所知,您的代码没问题。你能在你的代码中(而不是 ffmpeg)执行ls -s 吗?
猜你喜欢
  • 2017-10-01
  • 2013-11-12
  • 2019-03-30
  • 2021-08-15
  • 2012-05-23
  • 2017-08-30
  • 2017-10-01
  • 2014-10-27
  • 1970-01-01
相关资源
最近更新 更多