private void doTest() { using (WorkingDir wd = new WorkingDir()) { string mwFile = wd.makePath("muon.wav"); string seFile = wd.makePath("muon_stderr.txt"); File.Copy(getMuonWavFile(), mwFile); ProcessTools.runOnBatch("ffprobe.exe " + mwFile + " 2> " + seFile, binDir); if (hasAudioStream(seFile) == false) { throw new FaultOperation("ffmpeg を正しく実行出来ません。"); } } }
public static void doTest() { using (WorkingDir wd = new WorkingDir()) { string mwFile = wd.makePath("muon.wav"); string redirFile = wd.makePath(); File.Copy(muonWavFile, mwFile); ProcessTools.runOnBatch( "ffprobe.exe " + mwFile + " 2> " + redirFile, FFmpegBin.i.getBinDir() ); if (hasAudioStream(redirFile) == false) { throw new Exception("ffmpeg test error"); } } }
/// <summary> /// CurrencyPairs.FxCollect から呼んでね! /// </summary> public static void FxCollect() { ProcessTools.runOnBatch("FxCollect.bat", @"C:\app\Fx"); //ProcessTools.runOnBatch("FxCollect.bat", @"C:\Dev\Fx\GaitameOnlineMonitor\Tools"); // del @ 2020.7.16 }
private void convTh_main() { using (WorkingDir wd = new WorkingDir()) { string rExt = Path.GetExtension(_rFile); if (Gnd.i.audioVideoExtensions.contains(rExt) == false) { throw new Exception("再生可能なファイルではありません。(不明な拡張子)"); } string midFile = wd.makePath() + rExt; try { using (critSect.parallel()) { File.Copy(_rFile, midFile); } if (File.Exists(midFile) == false) { throw null; } } catch { throw new Exception("ファイルにアクセス出来ません。"); } string redirFile = wd.makePath(); ProcessTools.runOnBatch("ffprobe.exe " + _rFile + " 2> " + redirFile, FFmpeg.getBDir(), critSect); foreach (string line in FileTools.readAllLines(redirFile, Encoding.ASCII)) { if (line.Contains("Duration:")) { _duration = new Duration(); List <string> tokens = StringTools.tokenize(line, " :.,", false, true); if (tokens[1] == "N/A") { throw new Exception("再生可能なファイルではありません。(Duration)"); } int h = int.Parse(tokens[1]); int m = int.Parse(tokens[2]); int s = int.Parse(tokens[3]); int sec = h * 3600 + m * 60 + s; if (sec < 1) { throw new Exception("映像又は曲の長さが短すぎます。"); } if (IntTools.IMAX < sec) { throw new Exception("映像又は曲の長さが長すぎます。"); } _duration.secLength = sec; } else if (_audioStream == null && line.Contains("Stream") && line.Contains("Audio:")) { _audioStream = new AudioStream(); List <string> tokens = StringTools.tokenize(line, StringTools.DIGIT, true, true); _audioStream.mapIndex = int.Parse(tokens[1]); } else if (_videoStream == null && line.Contains("Stream") && line.Contains("Video:")) { _videoStream = new VideoStream(); { List <string> tokens = StringTools.tokenize(line, StringTools.DIGIT, true, true); _videoStream.mapIndex = int.Parse(tokens[1]); } { List <string> tokens = StringTools.tokenize(line, " ,"); foreach (string fToken in tokens) { string token = fToken; if (StringTools.toFormat(token, true) == "9x9") { List <string> whTokens = StringTools.tokenize(token, "x"); _videoStream.w = int.Parse(whTokens[0]); _videoStream.h = int.Parse(whTokens[1]); } } } if (_videoStream.w < Consts.VIDEO_W_MIN) { throw new Exception("映像の幅が小さすぎます。"); } if (_videoStream.h < Consts.VIDEO_H_MIN) { throw new Exception("映像の高さが小さすぎます。"); } if (IntTools.IMAX < _videoStream.w) { throw new Exception("映像の幅が大きすぎます。"); } if (IntTools.IMAX < _videoStream.h) { throw new Exception("映像の高さが大きすぎます。"); } } } if (_duration == null) { throw null; } if (_audioStream == null) { throw new Exception("再生可能なファイルではありません。(音声ストリームがありません)"); } if (_videoStream == null) { _type = Consts.MediaType_e.AUDIO; } else { _type = Consts.MediaType_e.MOVIE; } string wFile = Utils.getOgxFile(_wIndex, _type); if (Gnd.i.convWavMastering) { string wavFile = wd.makePath() + ".wav"; ProcessTools.runOnBatch( "ffmpeg.exe -i " + _rFile + " -map 0:" + _audioStream.mapIndex + " -ac 2 " + wavFile, FFmpeg.getBDir(), critSect ); if (File.Exists(wavFile) == false) { throw new Exception("音声ストリームの抽出に失敗しました。"); } string wmDir = wd.makePath(); string wmFile = Path.Combine(wmDir, "Master.exe"); string wavFileNew = wd.makePath() + ".wav"; Directory.CreateDirectory(wmDir); File.Copy(wavMasteringFile, wmFile); ProcessTools.runOnBatch( "Master.exe " + wavFile + " " + wavFileNew + " 0001.txt", wmDir, critSect ); if (File.Exists(wavFileNew) == false) { throw new Exception("wavFileNew does not exist"); } if (_type == Consts.MediaType_e.AUDIO) { ProcessTools.runOnBatch( "ffmpeg.exe -i " + wavFileNew + " -map 0:" + _audioStream.mapIndex + " " + Gnd.i.ffmpegOptAudio + " " + wFile, FFmpeg.getBDir(), critSect ); } else { ProcessTools.runOnBatch( "ffmpeg.exe -i " + _rFile + " -i " + wavFileNew + " -map 0:" + _videoStream.mapIndex + " -map 1:" + _audioStream.mapIndex + " " + Gnd.i.ffmpegOptVideo + " " + Gnd.i.ffmpegOptAudio + " " + wFile, FFmpeg.getBDir(), critSect ); } } else { if (_type == Consts.MediaType_e.AUDIO) { ProcessTools.runOnBatch( "ffmpeg.exe -i " + _rFile + " -map 0:" + _audioStream.mapIndex + " " + Gnd.i.ffmpegOptAudio + " " + wFile, FFmpeg.getBDir(), critSect ); } else { ProcessTools.runOnBatch( "ffmpeg.exe -i " + _rFile + " -map 0:" + _videoStream.mapIndex + " -map 0:" + _audioStream.mapIndex + " " + Gnd.i.ffmpegOptVideo + " " + Gnd.i.ffmpegOptAudio + " " + wFile, FFmpeg.getBDir(), critSect ); } } if (File.Exists(wFile) == false) { throw new Exception("wFile does not exist"); } _wFile = wFile; } }
public void perform() { using (WorkingDir wd = new WorkingDir()) { string rExt = Path.GetExtension(Gnd.i.rFile); if (Gnd.i.audioVideoExtensions.contains(rExt) == false) { throw new Exception("再生可能なファイルではありません。(不明な拡張子)"); } string midRFile = wd.makePath() + rExt; try { File.Copy(Gnd.i.rFile, midRFile); if (File.Exists(midRFile) == false) { throw null; } } catch { throw new Exception("ファイルにアクセス出来ません。"); } string redirFile = wd.makePath(); ProcessTools.runOnBatch("ffprobe.exe " + midRFile + " 2> " + redirFile, Gnd.i.ffmpegBinDir); foreach (string line in FileTools.readAllLines(redirFile, Encoding.ASCII)) { if (line.Contains("Duration:")) { _duration = new Duration(); List <string> tokens = StringTools.tokenize(line, " :.,", false, true); if (tokens[1] == "N/A") { throw new Exception("再生可能なファイルではありません。(Duration)"); } int h = int.Parse(tokens[1]); int m = int.Parse(tokens[2]); int s = int.Parse(tokens[3]); int sec = h * 3600 + m * 60 + s; if (sec < 1) { throw new Exception("映像又は曲の長さが短すぎます。"); } if (IntTools.IMAX < sec) { throw new Exception("映像又は曲の長さが長すぎます。"); } _duration.secLength = sec; } else if (_audioStream == null && line.Contains("Stream") && line.Contains("Audio:")) { _audioStream = new AudioStream(); List <string> tokens = StringTools.tokenize(line, StringTools.DIGIT, true, true); _audioStream.mapIndex = int.Parse(tokens[1]); } else if (_videoStream == null && line.Contains("Stream") && line.Contains("Video:")) { _videoStream = new VideoStream(); { List <string> tokens = StringTools.tokenize(line, StringTools.DIGIT, true, true); _videoStream.mapIndex = int.Parse(tokens[1]); } { List <string> tokens = StringTools.tokenize(line, " ,"); foreach (string token in tokens) { if (StringTools.toDigitFormat(token, true) == "9x9") { List <string> whTokens = StringTools.tokenize(token, "x"); _videoStream.w = int.Parse(whTokens[0]); _videoStream.h = int.Parse(whTokens[1]); } } } if (_videoStream.w < Consts.VIDEO_W_MIN) { throw new Exception("映像の幅が小さすぎます。"); } if (_videoStream.h < Consts.VIDEO_H_MIN) { throw new Exception("映像の高さが小さすぎます。"); } if (IntTools.IMAX < _videoStream.w) { throw new Exception("映像の幅が大きすぎます。"); } if (IntTools.IMAX < _videoStream.h) { throw new Exception("映像の高さが大きすぎます。"); } } } if (_duration == null) { throw null; } if (_audioStream == null) { throw new Exception("再生可能なファイルではありません。(音声ストリームがありません)"); } if (_videoStream == null) { _type = Consts.MediaType_e.AUDIO; } else { _type = Consts.MediaType_e.MOVIE; } string wFile = Utils.getFile(Gnd.i.wFileNoExt, _type); if (Utils.equalsExt(midRFile, wFile)) { try { File.Copy(midRFile, wFile, true); if (File.Exists(wFile) == false) { throw null; } } catch { throw new Exception("ファイルの出力に失敗しました。(単にコピー)"); } } else { string midWFile = wd.makePath() + Utils.getExt(_type); if (Gnd.i.convWavMastering) { string wavFile = wd.makePath() + ".wav"; ProcessTools.runOnBatch( "ffmpeg.exe -i " + midRFile + " -map 0:" + _audioStream.mapIndex + " -ac 2 " + wavFile, Gnd.i.ffmpegBinDir ); if (File.Exists(wavFile) == false) { throw new Exception("音声ストリームの抽出に失敗しました。"); } string wavFileNew = wd.makePath() + ".wav"; ProcessTools.runOnBatch( "Master.exe " + wavFile + " " + wavFileNew + " " + wd.makePath() + "_DMY_REP.tmp > " + redirFile, Gnd.i.wavMasterBinDir ); Utils.textFileToLog(redirFile, StringTools.ENCODING_SJIS); if (File.Exists(wavFileNew) == false) { throw new Exception("wavFileNew does not exist"); } if (_type == Consts.MediaType_e.AUDIO) { ProcessTools.runOnBatch( "ffmpeg.exe -i " + wavFileNew + " -map 0:0 " + Gnd.i.ffmpegOptAudio + " " + midWFile + " 2> " + redirFile, Gnd.i.ffmpegBinDir ); } else { ProcessTools.runOnBatch( "ffmpeg.exe -i " + midRFile + " -i " + wavFileNew + " -map 0:" + _videoStream.mapIndex + " -map 1:0 " + Gnd.i.ffmpegOptVideo + " " + Gnd.i.ffmpegOptAudio + " " + midWFile + " 2> " + redirFile, Gnd.i.ffmpegBinDir ); } } else { if (_type == Consts.MediaType_e.AUDIO) { ProcessTools.runOnBatch( "ffmpeg.exe -i " + midRFile + " -map 0:" + _audioStream.mapIndex + " " + Gnd.i.ffmpegOptAudio + " " + midWFile + " 2> " + redirFile, Gnd.i.ffmpegBinDir ); } else { ProcessTools.runOnBatch( "ffmpeg.exe -i " + midRFile + " -map 0:" + _videoStream.mapIndex + " -map 0:" + _audioStream.mapIndex + " " + Gnd.i.ffmpegOptVideo + " " + Gnd.i.ffmpegOptAudio + " " + midWFile + " 2> " + redirFile, Gnd.i.ffmpegBinDir ); } } Utils.textFileToLog(redirFile, Encoding.ASCII); if (File.Exists(midWFile) == false) { throw new Exception("midWFile does not exist"); } try { File.Copy(midWFile, wFile, true); if (File.Exists(wFile) == false) { throw null; } } catch { throw new Exception("ファイルの出力に失敗しました。(変換した)"); } } Gnd.i.convReturn.wFile = wFile; } if (_videoStream != null) { Gnd.i.convReturn.w = _videoStream.w; Gnd.i.convReturn.h = _videoStream.h; } }
private static void runCTools(string args) { ProcessTools.runOnBatch("CTools.exe " + args, Path.GetDirectoryName(cToolsFile)); }
private void loadFile() { string redirFile = _wd.makePath(); Gnd.i.progressMessage.post("入力ファイルをコピーしています..."); File.Copy(_origFile, _duplFile); Gnd.i.progressMessage.post("入力ファイルのフォーマットを調べています..."); ProcessTools.runOnBatch( "ffprobe.exe " + _duplFile + " 2> " + redirFile, FFmpegBin.i.getBinDir() ); foreach (string fLine in FileTools.readAllLines(redirFile, Encoding.ASCII)) { string line = fLine.Trim(); if (line.StartsWith("Stream")) { List <string> sInts = StringTools.tokenize(line, StringTools.DIGIT, true, true); int mapIndex = int.Parse(sInts[1]); List <string> tokens = StringTools.tokenize(line, " ,", false, true); if (line.Contains("Audio:")) { AudioStream stream = new AudioStream(); stream.mapIndex = mapIndex; _audioStreams.Add(stream); } else if (line.Contains("Video:")) { VideoStream stream = new VideoStream(); stream.mapIndex = mapIndex; { int index = ArrayTools.indexOf <string>(tokens.ToArray(), "fps", StringTools.comp); if (index == -1) { throw new Exception("映像ストリームの秒間フレーム数を取得出来ませんでした。"); } stream.fps = IntTools.toInt(double.Parse(tokens[index - 1])); } { string token = Utils.getTokenDigitFormat(tokens.ToArray(), "9x9"); if (token == null) { throw new Exception("映像ストリームの画面サイズを取得出来ませんでした。"); } List <string> s_wh = StringTools.tokenize(token, StringTools.DIGIT, true, true); stream.w = int.Parse(s_wh[0]); stream.h = int.Parse(s_wh[1]); } if (IntTools.isRange(stream.fps, 1, IntTools.IMAX) == false) { throw new FailedOperation("映像ストリームの秒間フレーム数を認識出来ません。" + stream.fps); } if (IntTools.isRange(stream.w, 1, IntTools.IMAX) == false) { throw new FailedOperation("映像ストリームの画面の幅を認識出来ません。" + stream.w); } if (IntTools.isRange(stream.h, 1, IntTools.IMAX) == false) { throw new FailedOperation("映像ストリームの画面の高さを認識出来ません。" + stream.h); } _videoStreams.Add(stream); } else { // "Data:" とか } } } if (_audioStreams.Count == 0) { throw new FailedOperation("音声ストリームがありません。"); } if (_videoStreams.Count == 0) { throw new FailedOperation("映像ストリームがありません。"); } _targetAudioStream = _audioStreams[0]; _targetVideoStream = _videoStreams[0]; // ---- Audio Stream ---- Gnd.i.progressMessage.post("音声ストリームを取り出しています..."); ProcessTools.runOnBatch( "ffmpeg.exe -i " + _duplFile + " -map 0:" + _targetAudioStream.mapIndex + " -ac 2 " + _wavFile + " 2> " + _wd.makePath("mk_wav_stderr.txt"), FFmpegBin.i.getBinDir() ); Gnd.i.progressMessage.post("音声ストリームを展開しています..."); _wavHz = CTools.wavFileToCsvFile(_wavFile, _wavCsvFile, _wd.makePath("mk_wav-csv_stdout.txt")); // 1 <= 音声の長さ < IMAX { long size = new FileInfo(_wavCsvFile).Length; if (size % 12L != 0) { throw new Exception("wav-csv data size error"); } long count = size / 12L; if (count == 0L) { throw new FailedOperation("音声ストリームに最初のサンプリング値がありません。"); } if (IntTools.IMAX <= count) { throw new FailedOperation("音声ストリームが長過ぎます。"); } } // ---- Video Stream ---- Gnd.i.progressMessage.post("映像ストリームを展開しています..."); ProcessTools.runOnBatch( "ffmpeg.exe -i " + _duplFile + " -map 0:" + _targetVideoStream.mapIndex + " -r " + _targetVideoStream.fps + " -f image2 -vcodec " + Consts.V_IMG_VCODEC + " " + _imgDir + "\\%%010d" + Consts.V_IMG_EXT + " 2> " + _wd.makePath("mk_img_stderr.txt"), FFmpegBin.i.getBinDir() ); // 1 <= 映像の長さ < IMAX if (File.Exists(_imgDir + "\\0000000001" + Consts.V_IMG_EXT) == false) { throw new FailedOperation("映像ストリームに最初のフレームがありません。"); } if (File.Exists(_imgDir + "\\1000000001" + Consts.V_IMG_EXT)) { throw new FailedOperation("映像ストリームが長過ぎます。"); } // ---- Gnd.i.progressMessage.post(""); // 完了 ed = new EditData(this); // VIDEO_W/H_MIN/MAX { Image img = ed.v.getImage(0); if (img.Width < Consts.VIDEO_W_MIN) { throw new FailedOperation("映像の幅が小さ過ぎます。" + img.Width); } if (Consts.VIDEO_W_MAX < img.Width) { throw new FailedOperation("映像の幅が大き過ぎます。" + img.Width); } if (img.Height < Consts.VIDEO_H_MIN) { throw new FailedOperation("映像の高さが小さ過ぎます。" + img.Height); } if (Consts.VIDEO_H_MAX < img.Height) { throw new FailedOperation("映像の高さが大き過ぎます。" + img.Height); } } // AUDIO_HZ_MIN/MAX { int hz = this._wavHz; if (hz < Consts.AUDIO_HZ_MIN) { throw new FailedOperation("音声ストリームのサンプリング周波数が小さ過ぎます。" + hz); } if (Consts.AUDIO_HZ_MAX < hz) { throw new FailedOperation("音声ストリームのサンプリング周波数が大き過ぎます。" + hz); } } }
private void doSave(string file) { string extNew = Path.GetExtension(file); if (Gnd.i.movieExtensions.contains(extNew) == false) { throw new FailedOperation("保存先ファイル名は不明な拡張子です。"); } using (WorkingDir wd = new WorkingDir()) { string wavFile = wd.makePath("1.wav"); string midFile = wd.makePath("2") + extNew; Gnd.i.progressMessage.post("音声ファイルを生成しています..."); CTools.csvFileToWavFile(md.getWavCsvFile(), wavFile, md.getWavHz(), wd.makePath("wr_wav_stdout.txt")); Gnd.i.progressMessage.post("映像ファイルを生成しています..."); string imgDir = md.getImageDir(); string vImgExt = Consts.V_IMG_EXT; if (Gnd.i._映像をJPEGで保存する) { string imgDirNew = wd.makePath(); Directory.CreateDirectory(imgDirNew); foreach (string vImgFile in Directory.GetFiles(imgDir)) { string wFile = Path.Combine(imgDirNew, Path.GetFileNameWithoutExtension(vImgFile)) + Consts.V_IMG_JPEG_EXT; using (Image bmp = Bitmap.FromFile(vImgFile)) using (EncoderParameters eps = new EncoderParameters(1)) using (EncoderParameter ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)Gnd.i._映像をJPEGで保存する時の画質)) { eps.Param[0] = ep; ImageCodecInfo ici = GetICI(ImageFormat.Jpeg); bmp.Save(wFile, ici, eps); } } imgDir = imgDirNew; vImgExt = Consts.V_IMG_JPEG_EXT; } Gnd.i.progressMessage.post("動画ファイルを生成しています..."); ProcessTools.runOnBatch( "ffmpeg.exe -r " + md.getTargetVideoStream().fps + " -i " + imgDir + "\\%%010d" + vImgExt + " -i " + wavFile + " -map 0:0 -map 1:0 " + Gnd.i.extensionsOptions.getOption(extNew) + " " + midFile + " 2> " + wd.makePath("wr_movie_stderr.txt"), FFmpegBin.i.getBinDir() ); if (File.Exists(midFile) == false) { throw new FailedOperation("動画ファイルの生成に失敗しました。"); } File.Copy(midFile, file, true); Gnd.i.progressMessage.post(""); // 完了 } }