• 締切済み

javaからffmpegの利用

ffmpegを呼び出してmp3変換を行いたいのですがずっと動いたまま終了せずに困っています。 import java.io.IOException; public class Cratemp3 { public static void main(String[] args) throws IOException, InterruptedException { ProcessBuilder processBuilder = new ProcessBuilder( "./ffmpeg", "-y" , "-i" , "sample.flv", "-acodec" , "copy" , "sample.mp3" ); Process process = processBuilder.start(); process.waitFor(); } } process.waitFor()を抜くと上手く動きますが process.waitFor()をいれると上手く動きません。 ffmpegで変換したmp3ファイルを使用して処理を続けたいのですがどうしたらいいのでしょうか? よろしくお願いします。

  • Java
  • 回答数1
  • ありがとう数6

みんなの回答

  • kotecho
  • ベストアンサー率66% (6/9)
回答No.1

サブプロセス(ffmpeg)が、メインプロセス(Java)の入力Streamに出力を行おうとしているが、 Process#waitForによりブロックされ、出口を失っている状態と思われます。 下記の様な処理をwaitForの前に追記してみて下さい。 ---- StringBuilder sb = new StringBuilder(); byte[] bin = new byte[1024]; InputStream is =process.getErrorStream(); // ffmpegのエラー出力 while (is.read(bin) >0) { sb.append(new String(bin)); } System.out.println(new String(sb)); // Process#getInputStreamも同様に。 // closeを忘れずに。 ---- 参考: http://java.sun.com/j2se/1.5.0/ja/docs/ja/api/java/lang/Process.html

関連するQ&A

  • javaからのFFMPEGの利用

    現在、windows vista に ffmpeg をインストールし、java(1.6)から以下のようにffmpeg.exeを呼び出しています。 -------------------------------------------------------------- public static void main(String[] args) { ProcessBuilder builder = new ProcessBuilder("C:/Tools/3GP_Converter034/cores/debug.bat"); try { Process process = builder.start(); InputStream is = process.getInputStream(); //標準出力 printInputStream(is); InputStream es = process.getErrorStream(); //標準エラー printInputStream(es); } catch (Exception e) { throw new ConversionException(e); } } public static void printInputStream(InputStream is) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(is)); for (;;) { String line = br.readLine(); if (line == null) break; System.out.println(line); } } -------------------------------------------------------------- debug.batの中身は以下です。 -------------------------------------------------------------- C:/Tools/3GP_Converter034/cores/ffmpeg.exe -y -i c:/work/a.mpg -f flv -vcodec flv -r 25 -b 900k -ar 44100 -ab 64k c:/work/out99.flv -------------------------------------------------------------- debug.batを直接呼び出すと正常にout99.flvが出力されるのですが、java経由だとうまくできません。 printInputStreamメソッドから以下のsysytem out文が出力されたまま止まってしまいます。 C:\java>C:/Tools/3GP_Converter034/cores/ffmpeg.exe -y -i c:/work/a.mpg -f flv -vcodec flv -r 25 -b 900k -ar 44100 -ab 64k c:/work/out99.flv まったく理由がわかりませn。 どなたかご教示ください;;

  • 【FFmpeg】FLVにWAVを格納できない

    ffmpegを用いてFLVコンテナーにPCM(WAV)を格納する際 44.1kHz/16bit(s16le)はMUXできますが例えば192kHz/24bit(s24le)ではエラーが出ます %~dp0ffmpeg -i %1 -i %2 -vcodec copy -acodec copy %~dpn1_out.flv とバッチを作って放り込んだだけですが これはffmpegの制限なのかFLVの仕様なのか私の方法がまずいのか分かりません ご教授よろしくお願いします

  • ProcessBuilderの使い方

    ProcessBuilderを使用してMysqlのコマンド(Select文、Create文)を実行することはできないか と思い下記サンプルを作ってみました。まづはmysqlを起動するコマンドを実行してみました。 Eclipse上で実行してみたのですが p.waitFor(); の部分で応答が返ってこなくなります。 何か使い方が間違っていますでしょうか。 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ProcessBuilderTest { public static void main(String[] args) { ProcessBuilder pb = new ProcessBuilder("mysql", "-u", "root", "-p", "root"); try { Process p = pb.start(); // ping が完了するのを待つ p.waitFor(); // 実行結果を取得するストリームの種別を出力 System.out.println(pb.redirectInput()); try (BufferedReader br = new BufferedReader( new InputStreamReader(p.getInputStream()))) { // ping結果の出力 for(String line = br.readLine(); line != null; line = br.readLine()) { System.out.println(line); } } } catch (IOException | InterruptedException e) { // 例外ハンドリング処理 } } }

  • どうしてもプログラムが動いてくれません....

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; public class Tese { public static void main(String[] args) throws InterruptedException, IOException { ★ ProcessBuilder pb = new ProcessBuilder("cmd", "/C", "dir", "C:\\Program Files\\Java"); ★ Process p = pb.start(); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); Catcher c = new Catcher(br); c.start(); p.waitFor(); p.destroy(); System.out.println(c.out.toString()); } } class Catcher extends Thread { Reader in; StringWriter out = new StringWriter(); public Catcher(Reader in) { this.in = in; } public void run() { int c; try { while ((c = in.read()) != -1) { out.write((char)c); } } catch (IOException e) { e.printStackTrace(); } } ★で囲んでいる部分なのですが,コンパイルすると Exception in thread "main" java.lang.Error: コンパイル問題が未解決です。 コンストラクター ProcessBuilder(String, String, String, String) は未定義です。 at Tese.main(Tese.java:10) というエラーが出ます. どうすればエラーが消えるかわかる方いらっしゃったら,ぜひ助言頂けないでしょうか・へj

    • ベストアンサー
    • Java
  • javaからexeの起動、値渡し(?)について。

    1. 短縮したソースで申し訳ないのですが、JavaからC++で作成したexeファイルの、 int secに、javaでexe起動時に任意の数値を渡したいのですが、 何か良い方法はありますでしょうか? 2. またJavaで、外部ファイルを起動した時に、例えばFirefox等の場合、 自身の設定や、お気に入りが全くない状態で起動するのは何故でしょうか? 回答いただければと思います。どうぞよろしくお願いいたします。 [java] ProcessBuilder pb=new ProcessBuilder("C:\\Users\\Super\\Desktop\\cplus.exe", "30"); try { Process p=pb.start(); int ret=p.waitFor(); System.out.println("process exited with value : " + ret); } catch (IOException ioe) { } catch (InterruptedException inte) { } [C++ win32api] #include <windows.h> int main() { int sec=0; HANDLE hTimer; BOOL ret=FALSE; DWORD err=0; //以下はタイマー作成 }

    • ベストアンサー
    • Java
  • javaでwgetを動作させる際に

    wget.exeを動作させると、日本語のファイルを取り出す際に文字が 化けてしまうため、java言語を用いてwget.exeを動作させ、文字 コードを指定することによって文字化けを解消させようと考えて おりますが、その方法がわかりません。 アドバイスの方お願いします。 以下は、現在できているソースです。 import java.io.*; public class wget { public static void main(String[] args) { String[] command = {"cmd.exe", "/c", "C:\\wget\\bin\\wget.exe", "-r", "-l", "1", "-k", "-np", "-nv", "http://dmoz.org/World/Japanese/"}; // 例外の発生を調べるブロック try { Runtime runtime = Runtime.getRuntime(); Process load_process = runtime.exec(command); load_process.waitFor(); } // 例外発生時の処理ブロック catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }

    • ベストアンサー
    • Java
  • バッチファイルの起動

    WEBの画面上からボタンをクリックして、バッチファイルを起動させる プログラムを作成しています。 プログラム・バッチ共ローカルで上記のテストを行なった所、正常にバッチを起動することができませんでした。 プログラム・バッチをサーバに移行し上記のテストを行なった所、バッチが起動せきませんでした。 プログラムは以下の通りです。どこが悪くてサーバ上でバッチが起動できないのでしょうか?。 宜しくお願い致します。     (JSP) <hr> <br> <table width="80%"> <tr> <td> <s:submit value=" 入力完了 " cssClass="form_button" action="TEST" method="doEnter" /> </td> </tr> </table> </s:form> </body> <html>     (JAVA) public String doEnter() throws Exception { boolean vCheckResult = true; String result; try { String sCmd = "D:\\bat\\jksys\\atelnet.bat"; ProcessBuilder builder = new ProcessBuilder(sCmd); Process process = builder.start(); int ret = process.waitFor(); System.out.println("process exited with value : " + ret); }catch (IOException e) { // start()で例外が発生 e.printStackTrace(); } catch (InterruptedException e) { // waitFor()で例外が発生 e.printStackTrace(); } return "success"; }

  • JavaでString型をChar型に変換するプログラムが分かりません

    JavaでString型をChar型に変換するプログラムが分かりません;; どなかた助けて下さい。 下のプログラムの何がいけないんでしょうか?? いろいろ間違ってるところはあると思うんですが、教えて下さい! import java.io.*; class { public static void main(String args[]) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str1 = br.readLine(); StringBuffer moji = new StringBuffer(str1); char nyuu=moji;

  • Javaが途中で止まってしまいます

    Javaで音声ファイルを変換するスクリプトなのですが、音声ファイル自体は認識して「ファイルコピー終了」と出るのですが、実際にはコピーされておらず、そこで終わってしまい変換工程まで辿り着かない状態です。 ============================== import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class OnseiFileConvert { /** * @param args */ public static void main(String[] args) { OnseiFileConvert exe = new OnseiFileConvert(); exe.execute(); } /** m = 0x80; for( i=0; i<fsize; i++, m++ ) { if( m == 0xFF ) { m = 0x80; } fread( &t, 1, 1, fp ); t = t^m; fwrite( &t, 1, 1, fp_out ); } */ public void execute(){ String voice_dir_in = "C:\\java\\onsei"; String voice_dir_out = "C:\\output\\onsei"; try { convVoice(voice_dir_in,voice_dir_out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 音声変換処理 * @throws IOException * @throws InterruptedException */ private void convVoice(String voice_dir_in,String voice_dir_out) throws IOException, InterruptedException{ System.out.println("ディレクトリーコピー開始"); System.out.println("xcopy /T /E \"" + voice_dir_in + "\" \"" + voice_dir_out +"\""); Process proc = Runtime.getRuntime().exec( "xcopy /T \"" + voice_dir_in + "\" \"" + voice_dir_out +"\""); proc.waitFor(); proc.destroy(); System.out.println("ディレクトリーコピー終了"); System.out.println("ファイルコピー開始"); convData(voice_dir_in, voice_dir_out); System.out.println("ファイルコピー終了"); } /** * @param voice_dir_in * @param voice_dir_out * @throws IOException * @throws InterruptedException */ private void convData(String voice_dir_in, String voice_dir_out) throws IOException, InterruptedException { System.out.println(voice_dir_in + "/" + voice_dir_out); File in_file = new File(voice_dir_in); File[] in_files_list = in_file.listFiles(); for(int i = 0; i < in_files_list.length ; i++ ){ File chFile = in_files_list[i]; if(chFile.isDirectory()){ convData(voice_dir_in + "\\" + chFile.getName() ,voice_dir_out + "\\" + chFile.getName() ); } else { String file_name = chFile.getName(); System.out.println(file_name); if(".".equals(file_name.substring(0, 1))){ file_name = file_name.substring(1, file_name.length()-4); File ofile = new File(voice_dir_out + "\\" + file_name); if( ofile.length() == 0 ){ convFile(voice_dir_in + "\\" + chFile.getName(),voice_dir_out + "\\" + file_name); } } } } } /** * ファイル単位に変換を実施する処理 */ private void convFile(String input_file_name,String out_file_name) { //convert FileInputStream fip = null; FileOutputStream fop = null; try { System.out.println("変換開始"); fip = new FileInputStream(input_file_name); fop = new FileOutputStream(out_file_name); long fsize = (new File(input_file_name)).length(); int m = 0x80; for( int i=0; i< fsize; i++, m++ ) { if( m == 0xFF ) { m = 0x80; } int t = fip.read(); t = t^m; fop.write(t); } System.out.println("変換完了" + fsize + "Byte"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(fip != null){ fip.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(fop != null){ fop.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } ============================== 長時間悩んで書き直したりしたのですが、やはり結果が変わらずお手上げ状態です。 どうぞご助力お願いします。

    • ベストアンサー
    • Java
  • 今勉強中のjavaで。わからないことがあります。

    import java.io.*; class Ireru { public static void main(String args[]) throws IOException{ System.out.println("あなたは何歳ですか?"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str1 = br.readLine(); int num =Integer.parseInt(str1); System.out.println("あなたは" + num +"歳です。"); } } ------------------------------- import java.io.*; class Ireru { public static void main(String args[]) throws IOException{ System.out.println("あなたは何歳ですか?"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str1 = br.readLine(); System.out.println("あなたは" + str1 +"歳です。"); } } なぜ下では悪いのですか?なぜnum をつかないといけないかがわかりません。正確には str1とnumの違いがわからなくて困っています。何が混乱の原因ですか?

    • ベストアンサー
    • Java

専門家に質問してみよう