예제 #1
0
파일: Utils.cs 프로젝트: stackprobe/Kirara2
        public static string getExt(Consts.MediaType_e type)
        {
            switch (type)
            {
            case Consts.MediaType_e.AUDIO: return(".ogg");

            case Consts.MediaType_e.MOVIE: return(".ogv");
            }
            throw null;
        }
예제 #2
0
파일: Conv.cs 프로젝트: stackprobe/Kirara2
        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;
            }
        }
예제 #3
0
파일: Conv.cs 프로젝트: stackprobe/Kirara2
        private void doConv()
        {
            addOperation(delegate
            {
                rExt = Path.GetExtension(_rFile);

                if (Gnd.i.audioVideoExtensions.contains(rExt) == false)
                {
                    throw new Exception("再生可能なファイルではありません。(不明な拡張子)");
                }

                midRFile = _wd.makePath() + rExt;

                {
                    long rFileSize = -1;

                    try
                    {
                        rFileSize = new FileInfo(_rFile).Length;
                    }
                    catch
                    { }

                    if (Gnd.i.rFileSizeMax_MB * 1000000L < rFileSize)
                    {
                        throw new Exception("ファイルが大きすぎます。");
                    }
                }

                {
                    long diskFree = new DriveInfo(FileTools.getTMP().Substring(0, 1)).AvailableFreeSpace;

                    if (diskFree < Gnd.i.keepDiskFree_MB * 1000000L)
                    {
                        throw new Exception("ディスクの空き領域が不足しています。");
                    }
                }

                runCopyFile(_rFile, midRFile, "ファイルにアクセス出来ません。");
            });
            addOperation(delegate
            {
                redirFile = _wd.makePath();

                runCommand("ffprobe.exe " + midRFile + " 2> " + redirFile, Gnd.i.ffmpeg.binDir);
            });
            addOperation(delegate
            {
                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 new Exception("fatal: ffprobe _duration null");
                }

                if (_audioStream == null)
                {
                    throw new Exception("再生可能なファイルではありません。(音声ストリームがありません)");
                }

                if (_duration.secLength < 3)
                {
                    throw new Exception("再生時間が短すぎます。");
                }

                if (_videoStream == null)
                {
                    _type = Consts.MediaType_e.AUDIO;
                }
                else
                {
                    _type = Consts.MediaType_e.MOVIE;
                }

                wFile = _wFileNoExt + (_type == Consts.MediaType_e.AUDIO ? ".ogg" : ".ogv");

                if (StringTools.equalsIgnoreCase(rExt, Path.GetExtension(wFile)))
                {
                    doConvMode = false;
                    midWFile   = midRFile;
                }
                else
                {
                    doConvMode = true;
                    midWFile   = _wd.makePath() + Path.GetExtension(wFile);
                }
            });
            if (Gnd.i.convWavMastering)
            {
                addOperation(delegate
                {
                    if (doConvMode)
                    {
                        wavFile = _wd.makePath() + ".wav";

                        runCommand(
                            "ffmpeg.exe -i " + midRFile + " -map 0:" + _audioStream.mapIndex + " -ac 2 " + wavFile,
                            Gnd.i.ffmpeg.binDir
                            );
                    }
                });
                addOperation(delegate
                {
                    if (doConvMode)
                    {
                        if (File.Exists(wavFile) == false)
                        {
                            throw new Exception("音声ストリームの抽出に失敗しました。");
                        }

                        wavFileNew = _wd.makePath() + ".wav";

                        runCommand(
                            "Master.exe " + wavFile + " " + wavFileNew + " 0001.txt > " + redirFile,
                            Gnd.i.wavMaster.binDir
                            );
                    }
                });
                addOperation(delegate
                {
                    if (doConvMode)
                    {
                        reportToLog(redirFile, StringTools.ENCODING_SJIS);

                        if (File.Exists(wavFileNew) == false)                         // ? 音量の均一化 不要だった。
                        {
                            Gnd.i.logger.writeLine("wavFileNew <- wavFile");
                            wavFileNew = wavFile;
                        }
                        if (_type == Consts.MediaType_e.AUDIO)
                        {
                            runCommand(
                                "ffmpeg.exe -i " + wavFileNew + " -map 0:0 " + Gnd.i.ffmpegOptAudio + " " + midWFile + " 2> " + redirFile,
                                Gnd.i.ffmpeg.binDir
                                );
                        }
                        else
                        {
                            runCommand(
                                "ffmpeg.exe -i " + midRFile + " -i " + wavFileNew + " -map 0:" + _videoStream.mapIndex + " -map 1:0 " + Gnd.i.ffmpegOptVideo + " " + Gnd.i.ffmpegOptAudio + " " + midWFile + " 2> " + redirFile,
                                Gnd.i.ffmpeg.binDir
                                );
                        }
                    }
                });
            }
            else
            {
                addOperation(delegate
                {
                    if (doConvMode)
                    {
                        if (_type == Consts.MediaType_e.AUDIO)
                        {
                            runCommand(
                                "ffmpeg.exe -i " + midRFile + " -map 0:" + _audioStream.mapIndex + " " + Gnd.i.ffmpegOptAudio + " " + midWFile + " 2> " + redirFile,
                                Gnd.i.ffmpeg.binDir
                                );
                        }
                        else
                        {
                            runCommand(
                                "ffmpeg.exe -i " + midRFile + " -map 0:" + _videoStream.mapIndex + " -map 0:" + _audioStream.mapIndex + " " + Gnd.i.ffmpegOptVideo + " " + Gnd.i.ffmpegOptAudio + " " + midWFile + " 2> " + redirFile,
                                Gnd.i.ffmpeg.binDir
                                );
                        }
                    }
                });
            }
            addOperation(delegate
            {
                if (doConvMode)
                {
                    reportToLog(redirFile, Encoding.ASCII);
                }
            });
            addOperation(delegate
            {
                try
                {
                    string dir = Path.GetDirectoryName(wFile);

                    Gnd.i.logger.writeLine("dir: " + dir);

                    if (Directory.Exists(dir) == false)
                    {
                        Directory.CreateDirectory(dir);
                    }
                }
                catch
                {
                    throw new Exception("ファイルを出力できません。(フォルダ作成失敗)");
                }

                runCopyFile(midWFile, wFile, "ファイルを出力できません。");
            });
        }
예제 #4
0
        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;
            }
        }
예제 #5
0
파일: Utils.cs 프로젝트: stackprobe/Kirara2
 public static string getOgxFile(int index, Consts.MediaType_e type)
 {
     return(Path.Combine(getTMP(), Consts.MEDIA_DIR_ID, StringTools.zPad(index, 10)) + (type == Consts.MediaType_e.AUDIO ? ".ogg" : ".ogv"));
 }
예제 #6
0
파일: Utils.cs 프로젝트: stackprobe/Kirara2
 public static string getFile(string fileNoExt, Consts.MediaType_e type)
 {
     return(fileNoExt + getExt(type));
 }