Exemple #1
0
        public void Test01()
        {
            PictureData picture = new PictureData(new Canvas2(@"C:\wb2\20191204_ジャケット的な\バンドリ_イニシャル.jpg"), 1920, 1080);

            const int FRAME_NUM = 200;

            const int D_MAX = 20;
            int       dTarg = D_MAX;
            int       d     = D_MAX;

            using (WorkingDir wd = new WorkingDir())
            {
                FileTools.CreateDir(wd.GetPath("img"));

                for (int frame = 0; frame < FRAME_NUM; frame++)
                {
                    Console.WriteLine(frame);                     // test

                    picture.SetFrame(frame * 1.0 / FRAME_NUM);

                    // ---- 暗くする ----

                    if (frame == 10)
                    {
                        dTarg = 0;
                    }
                    else if (frame + 10 + D_MAX == FRAME_NUM)
                    {
                        dTarg = D_MAX;
                    }

                    if (d < dTarg)
                    {
                        d++;
                    }
                    else if (dTarg < d)
                    {
                        d--;
                    }

                    if (0 < d)
                    {
                        picture.SetDrakness(d * 1.0 / D_MAX);
                    }

                    // ----

                    picture.Save(wd.GetPath("img\\" + frame + ".jpg"));
                }

                ProcessTools.Batch(new string[]
                {
                    @"C:\app\ffmpeg-4.1.3-win64-shared\bin\ffmpeg.exe -r 20 -i %%d.jpg ..\out.mp4",
                },
                                   wd.GetPath("img")
                                   );

                File.Copy(wd.GetPath("out.mp4"), @"C:\temp\1.mp4", true);
            }
        }
Exemple #2
0
        // ----

        public void MakeMovie()
        {
            if (this.Audio == null)
            {
                throw new Exception("Audio is null");
            }

            FileTools.Delete(this.GetVideoFile());
            FileTools.Delete(this.GetMovieFile());

            ProcessTools.Batch(new string[]
            {
                string.Format(@"{0}ffmpeg.exe -r {1} -i %%d.jpg ..\video.mp4",
                              AudioPicMP4Props.FFmpegPathBase,
                              AudioPicMP4Props.FPS
                              ),
            },
                               this.GetImageDir()
                               );

            if (File.Exists(this.GetVideoFile()) == false)
            {
                throw new Exception();
            }

            ProcessTools.Batch(new string[]
            {
                string.Format(@"{0}ffmpeg.exe -i video.mp4 -i {1} -map 0:0 -map 1:{2} -vcodec copy -codec:a copy movie.mp4",
                              AudioPicMP4Props.FFmpegPathBase,
                              this.Audio.GetFile(),
                              this.Audio.GetInfo().GetAudioStreamIndex()
                              ),
            },
                               this.WD.GetPath(".")
                               );

            if (Utils.IsEmptyFile(this.GetMovieFile()))
            {
                FileTools.Delete(this.GetMovieFile());

                ProcessTools.Batch(new string[]
                {
                    // -codec:a copy を除去
                    string.Format(@"{0}ffmpeg.exe -i video.mp4 -i {1} -map 0:0 -map 1:{2} -vcodec copy movie.mp4",
                                  AudioPicMP4Props.FFmpegPathBase,
                                  this.Audio.GetFile(),
                                  this.Audio.GetInfo().GetAudioStreamIndex()
                                  ),
                },
                                   this.WD.GetPath(".")
                                   );

                if (Utils.IsEmptyFile(this.GetMovieFile()))
                {
                    throw new Exception();
                }
            }
        }
        private static void Run(string command)
        {
            command += " 1> stdout.tmp 2> stderr.tmp";

            ProcMain.WriteLog("command: " + command);

            ProcessTools.Batch(new string[] { command });

            ProcMain.WriteLog("stdout.tmp ----> " + File.ReadAllText("stdout.tmp", StringTools.ENCODING_SJIS) + " <---- ここまで");
            ProcMain.WriteLog("stderr.tmp ----> " + File.ReadAllText("stderr.tmp", StringTools.ENCODING_SJIS) + " <---- ここまで");
        }
Exemple #4
0
        private void Batch(string command, string workingDir)
        {
            Ground.I.Logger.Info("Batch.1");
            Ground.I.Logger.Info("command: " + command);
            Ground.I.Logger.Info("workingDir: " + workingDir);

            this.CheckCancel();
            Ground.I.Logger.Info("Batch.2");
            ProcessTools.Batch(new string[] { command }, workingDir);
            Ground.I.Logger.Info("Batch.3");
            this.CheckCancel();

            Ground.I.Logger.Info("Batch.4");
        }
Exemple #5
0
        public WaveData(string wavFile)
        {
            using (WorkingDir wd = new WorkingDir())
            {
                File.Copy(wavFile, wd.GetPath("1.wav"));

                ProcessTools.Batch(new string[]
                {
                    Consts.wavCsv_FILE + " /W2C 1.wav 1.csv 1.hz",
                },
                                   wd.GetPath(".")
                                   );

                using (CsvFileReader reader = new CsvFileReader(wd.GetPath("1.csv")))
                {
                    List <double> wavData_L = new List <double>();
                    List <double> wavData_R = new List <double>();

                    for (; ;)
                    {
                        string[] row = reader.ReadRow();

                        if (row == null)
                        {
                            break;
                        }

                        // memo: row[0,1]の値域は0~65535だが、32768が波形0(無音?)なので、これを0にするために65536で割る。

                        // 波形を [-1.0, 1.0) の区間にする。
                        wavData_L.Add((int.Parse(row[0]) / 65536.0 - 0.5) * 2.0);
                        wavData_R.Add((int.Parse(row[1]) / 65536.0 - 0.5) * 2.0);
                    }
                    //if (wavData_R.Count == 0)
                    if (wavData_L.Count == 0)
                    {
                        throw new Exception("wavData_L.Count == 0");
                    }

                    this.WavData_L = wavData_L.ToArray();
                    this.WavData_R = wavData_R.ToArray();
                }
                this.WavHz = int.Parse(File.ReadAllText(wd.GetPath("1.hz"), Encoding.ASCII));

                if (this.WavHz < 1 || IntTools.IMAX < this.WavHz)
                {
                    throw new Exception("Bad WavHz: " + this.WavHz);
                }
            }
        }
Exemple #6
0
        public static void MP3FileToWavFile(string rMP3File, string wWavFile)
        {
            if (string.IsNullOrEmpty(rMP3File))
            {
                throw null;
            }

            if (string.IsNullOrEmpty(wWavFile))
            {
                throw null;
            }

            FileTools.Delete(wWavFile);

            if (File.Exists(rMP3File) == false)
            {
                throw null;
            }

            if (File.Exists(wWavFile))
            {
                throw null;
            }

            using (WorkingDir wd = new WorkingDir())
            {
                File.Copy(rMP3File, wd.GetPath("1.mp3"));

                ProcessTools.Batch(new string[]
                {
                    Consts.FFMPEG_FILE + " -i 1.mp3 2.wav",
                },
                                   wd.GetPath(".")
                                   );

                if (File.Exists(wd.GetPath("2.wav")) == false)
                {
                    throw new Exception("変換に失敗しました。");
                }

                File.Copy(wd.GetPath("2.wav"), wWavFile);

                if (File.Exists(wWavFile) == false)
                {
                    throw new Exception("コピーに失敗しました。");
                }
            }
        }
Exemple #7
0
        public WaveData(string wavFile)
        {
            using (WorkingDir wd = new WorkingDir())
            {
                File.Copy(wavFile, wd.GetPath("1.wav"));

                ProcessTools.Batch(new string[]
                {
                    Ground.I.wavCsvExeFile + " /W2C 1.wav 1.csv 1.hz",
                },
                                   wd.GetPath(".")
                                   );

                using (CsvFileReader reader = new CsvFileReader(wd.GetPath("1.csv")))
                {
                    List <double> wavData_L = new List <double>();
                    List <double> wavData_R = new List <double>();

                    for (; ;)
                    {
                        string[] row = reader.ReadRow();

                        if (row == null)
                        {
                            break;
                        }

                        wavData_L.Add((int.Parse(row[0]) / 65536.0 - 0.5) * 2.0);
                        wavData_R.Add((int.Parse(row[1]) / 65536.0 - 0.5) * 2.0);
                    }
                    if (wavData_L.Count == 0)
                    {
                        throw new Exception("wavData_L.Count == 0");
                    }

                    this.WavData_L = wavData_L.ToArray();
                    this.WavData_R = wavData_R.ToArray();
                }
                this.WavHz = int.Parse(File.ReadAllText(wd.GetPath("1.hz"), Encoding.ASCII));

                if (this.WavHz < 1 || IntTools.IMAX < this.WavHz)
                {
                    throw new Exception("Bad WavHz: " + this.WavHz);
                }
            }
        }
Exemple #8
0
        public static bool Mastering(string sourceWavFile, string destWavFile, Action <string[]> writeReport = null)
        {
            FileTools.Delete(destWavFile);

            using (WorkingDir wd = new WorkingDir())
            {
                File.Copy(sourceWavFile, wd.GetPath("in.wav"));

                ProcessTools.Batch(new string[]
                {
                    string.Format(@"{0} /E {1} in.wav out.wav report.txt",
                                  Ground.I.MasterExeFile,
                                  CANCEL_EV_NAME
                                  ),
                },
                                   wd.GetPath(".")
                                   );

                {
                    string reportFile = wd.GetPath("report.txt");

                    if (File.Exists(reportFile) == false)
                    {
                        throw new Exception("[Mastering]レポートファイルが出力されませんでした。");
                    }

                    if (writeReport != null)
                    {
                        writeReport(File.ReadAllLines(reportFile, StringTools.ENCODING_SJIS));
                    }
                }

                {
                    string midFile = wd.GetPath("out.wav");

                    if (File.Exists(midFile) == false)
                    {
                        return(false);
                    }

                    File.Copy(midFile, destWavFile);
                }
            }
            return(true);
        }
Exemple #9
0
        private void MakeMovie_Main(string mediaFile, string destMP4File, bool masterFlag, IVideoMaker videoMaker)
        {
            WaveData wave;

            {
                string wavFile       = this.WD.GetPath("audio.wav");
                string masterWavFile = this.WD.GetPath("audio2.wav");

                FFmpegConv.MakeWavFile(mediaFile, wavFile);

                if (masterFlag && MasterUtils.Mastering(wavFile, masterWavFile))
                {
                    wave = new WaveData(masterWavFile);
                    this.FFmpeg.Audio.PutAudioFile(masterWavFile);
                }
                else
                {
                    wave = new WaveData(wavFile);
                    this.FFmpeg.Audio.PutAudioFile(mediaFile);
                }
            }

            int frame = 0;

            using (WorkingDir mvWd = new WorkingDir())
            {
                videoMaker.MakeVideo(wave, mvWd, image =>
                {
                    image.Save(this.FFmpeg.GetImageFile(frame), ImageFormat.Jpeg, AudioPicMP4Props.JPEG_QUALITY);
                    frame++;

                    GC.Collect();
                });
            }

            this.FFmpeg.MakeMovie();

            ProcessTools.Batch(new string[]
            {
                string.Format(@"ECHO Y|CACLS {0} /P Users:F Guest:F", this.FFmpeg.GetMovieFile()),
            });

            File.Move(this.FFmpeg.GetMovieFile(), destMP4File);
        }
Exemple #10
0
        public static void MakeWavFile(string movieOrAudioFile, string destWavFile)
        {
            FileTools.Delete(destWavFile);

            if (Path.GetExtension(movieOrAudioFile).ToLower() == ".wav")
            {
                File.Copy(movieOrAudioFile, destWavFile);
            }
            else
            {
                using (FFmpegMedia audio = new FFmpegMedia())
                    using (WorkingDir wd = new WorkingDir())
                    {
                        audio.PutAudioFile(movieOrAudioFile);

                        ProcessTools.Batch(new string[]
                        {
                            // ステレオにする。
                            string.Format(@"{0}ffmpeg.exe -i {1} -map 0:{2} -ac 2 out.wav",
                                          AudioPicMP4Props.FFmpegPathBase,
                                          audio.GetFile(),
                                          audio.GetInfo().GetAudioStreamIndex()
                                          ),
                        },
                                           wd.GetPath(".")
                                           );

                        {
                            string midFile = wd.GetPath("out.wav");

                            if (File.Exists(midFile) == false)
                            {
                                throw new Exception();
                            }

                            File.Copy(midFile, destWavFile);
                        }
                    }
            }
        }
Exemple #11
0
        public void Test01_a(string wavFile, string imgFile, string mp4File)
        {
            using (WorkingDir wd = new WorkingDir())
            {
                string imgDir = wd.GetPath("img");

                FileTools.CreateDir(imgDir);

                PictureData picture = new PictureData(new Canvas2(imgFile), 1920, 1080);
                WaveData    wave    = new WaveData(wavFile);

                VideoData video  = new VideoData(picture, (wave.Length * VideoData.FPS) / wave.WavHz, imgDir);
                IEffect   effect = new SpectrumEffect01(wave);

                video.MakeImages(new VideoData.FadeInOutInfo()
                {
                    StartMargin   = 2 * VideoData.FPS,
                    EndMargin     = -1,
                    FadeInOutSpan = 20,
                },
                                 effect,
                                 new VideoData.FadeInOutInfo()
                {
                    StartMargin   = -1,
                    EndMargin     = 10,
                    FadeInOutSpan = 10,
                });

                File.Copy(wavFile, wd.GetPath("audio.wav"));

                ProcessTools.Batch(new string[]
                {
                    @"C:\app\ffmpeg-4.1.3-win64-shared\bin\ffmpeg.exe -r 20 -i %%d.jpg -i ..\audio.wav ..\out.mp4",
                },
                                   imgDir
                                   );

                File.Copy(wd.GetPath("out.mp4"), mp4File, true);
            }
        }
Exemple #12
0
        public void Perform(string inputDir, string outputDir, bool outputOverwriteMode, int threadCount)
        {
            try
            {
                Ground.I.CmProgressRate.Clear();
                Ground.I.CmReport.Clear();

                using (WorkingDir wd = new WorkingDir())
                {
                    string        successfulFile = wd.MakePath();
                    double        progressRate   = 0.0;
                    List <string> reportBuff     = new List <string>();

                    WaitDlgTools.Show(
                        Consts.CONV_PROCESSING_TITLE,
                        Consts.CONV_PROCESSING_MESSAGE_NORMAL,
                        () =>
                    {
                        ProcessTools.Batch(new string[]
                        {
                            string.Format(
                                Ground.I.ConvExeFile + " CS-Conv {0} {1} {2} {3} \"{4}\""
                                , StringTools.LiteEncode(inputDir)
                                , StringTools.LiteEncode(outputDir)
                                , outputOverwriteMode ? 1 : 0
                                , threadCount
                                , successfulFile
                                ),
                        },
                                           ProcMain.SelfDir
                                           );
                    },
                        () =>
                    {
                        if (Ground.I.EvCancellable_Y.WaitForMillis(0))
                        {
                            WaitDlg.Cancellable = true;
                        }
                        if (Ground.I.EvCancellable_N.WaitForMillis(0))
                        {
                            WaitDlg.Cancellable = false;
                        }
                        if (Ground.I.EvMessage_Normal.WaitForMillis(0))
                        {
                            WaitDlg.MessagePost.Post(Consts.CONV_PROCESSING_MESSAGE_NORMAL);
                        }
                        if (Ground.I.EvMessage_StartGenVideo.WaitForMillis(0))
                        {
                            WaitDlg.MessagePost.Post(Consts.CONV_PROCESSING_MESSAGE_START_GEN_VIDEO);
                        }
                        if (Ground.I.EvMessage_GenVideoRunning.WaitForMillis(0))
                        {
                            WaitDlg.MessagePost.Post(Consts.CONV_PROCESSING_MESSAGE_GEN_VIDEO_RUNNING);
                        }

                        {
                            byte[] message = Ground.I.CmProgressRate.Recv();

                            if (message != null)
                            {
                                progressRate = double.Parse(Encoding.ASCII.GetString(message));
                            }
                        }

                        {
                            byte[] message = Ground.I.CmReport.Recv();

                            if (message != null)
                            {
                                reportBuff.Add(Encoding.UTF8.GetString(message));

                                while (REPORT_BUFF_MAX < reportBuff.Count)
                                {
                                    reportBuff.RemoveAt(0);
                                }

                                WaitDlg.DetailMessagePost.Post(reportBuff.ToArray());
                            }
                        }

                        return(progressRate);
                    },
                        () =>
                    {
                        Ground.I.EvStop_Conv.Set();
                        Ground.I.EvStop_Master.Set();
                    });

                    if (File.Exists(successfulFile) == false)
                    {
                        throw new Exception("変換プロセスは正常に動作しませんでした。");
                    }

                    bool userCancelled = Ground.I.EvMessage_UserCancelled.WaitForMillis(0);

                    if (WaitDlg.LastCancelled || userCancelled)
                    {
                        MessageDlgTools.Show(MessageDlg.Mode_e.Warning, "変換中止", "変換プロセスを中止しました。(" + (userCancelled ? 1 : 0) + ")");
                    }
                    else
                    {
                        MessageDlgTools.Information("変換完了", "変換プロセスは終了しました。");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageDlgTools.Error("変換処理エラー", ex);
            }
        }
Exemple #13
0
        private void Main2(ArgsReader ar)
        {
            string        resDir    = null;
            List <string> srcDirs   = new List <string>();
            string        outJSFile = null;
            string        tagsFile  = null;

            while (true)
            {
                if (ar.ArgIs("/R"))
                {
                    resDir = ar.NextArg();
                    continue;
                }
                if (ar.ArgIs("/S"))
                {
                    srcDirs.Add(ar.NextArg());
                    continue;
                }
                if (ar.ArgIs("/WB"))
                {
                    outJSFile  = ar.NextArg();
                    outJSFile  = FileTools.MakeFullPath(outJSFile);
                    outJSFile += ".js";
                    continue;
                }
                if (ar.ArgIs("/T"))
                {
                    tagsFile = ar.NextArg();
                    continue;
                }
                if (ar.HasArgs())
                {
                    throw new Exception("不明なコマンド引数");
                }

                break;
            }

            if (resDir == null)
            {
                throw new Exception("リソースディレクトリを指定して下さい。");
            }

            if (srcDirs.Count == 0)
            {
                throw new Exception("ソースディレクトリを指定して下さい。");
            }

            if (outJSFile == null)
            {
                throw new Exception("出力JSファイルを指定して下さい。");
            }

            if (tagsFile == null)
            {
                throw new Exception("tagsファイルを指定して下さい。");
            }

            {
                JSModuleConverter mc = new JSModuleConverter();

                mc.LoadResourceDir(resDir);

                foreach (string srcDir in srcDirs)
                {
                    mc.LoadSourceDir(srcDir);
                }

                mc.WriteJSFile(outJSFile);
            }

            using (WorkingDir wd = new WorkingDir())
            {
                const string MID_TAGS_FILE = "tags.mid.tmp";

                ProcessTools.Batch(new string[]
                {
                    string.Format(@"C:\Factory\DevTools\makeTags.exe /JS {0} {1}", outJSFile, MID_TAGS_FILE),
                },
                                   wd.GetPath(".")
                                   );

                if (File.Exists(wd.GetPath(MID_TAGS_FILE)) == false)
                {
                    throw new Exception("tagsファイルの生成に失敗しました。");
                }

                string[] tagsLines = File.ReadAllLines(wd.GetPath(MID_TAGS_FILE), StringTools.ENCODING_SJIS);

                File.AppendAllLines(tagsFile, tagsLines, StringTools.ENCODING_SJIS);                 // 注意:追記する。
            }
        }
Exemple #14
0
        private void Main_Go(string audioDir, string videoDir, string wDir)
        {
            if (Directory.Exists(audioDir) == false)
            {
                throw null;
            }

            if (Directory.Exists(videoDir) == false)
            {
                throw null;
            }

            string midVideoDir = Path.Combine(wDir, "video");

            FileTools.Delete(wDir);
            FileTools.CreateDir(wDir);
            FileTools.CreateDir(midVideoDir);

            string audioWavFile = Path.Combine(audioDir, "Wave.wav");

            if (File.Exists(audioWavFile) == false)
            {
                throw null;
            }

            int frame;

            Console.WriteLine("*1");             // test

            for (frame = 0; ; frame++)
            {
                string rFile = Path.Combine(videoDir, string.Format("{0}.bmp", frame));
                string wFile = Path.Combine(midVideoDir, string.Format("{0}.jpg", frame));

                if (File.Exists(rFile) == false)
                {
                    break;
                }

                if (frame % 100 == 0)
                {
                    Console.WriteLine("frame: " + frame);                     // test
                }
                new Canvas2(rFile).Save(wFile, ImageFormat.Jpeg, 90);
            }
            if (frame < 1)
            {
                throw null;
            }

            Console.WriteLine("*2");             // test

            ProcessTools.Batch(new string[]
            {
                Consts.FFMPEG_FILE + " -r 20 -i %%d.jpg ..\\video.mp4",
            },
                               midVideoDir
                               );

            if (File.Exists(Path.Combine(wDir, "video.mp4")) == false)
            {
                throw null;
            }

            Console.WriteLine("*3");             // test

            ProcessTools.Batch(new string[]
            {
                Consts.FFMPEG_FILE + " -i video.mp4 -i " + audioWavFile + " -map 0:0 -map 1:0 -vcodec copy movie.mp4",
            },
                               wDir
                               );

            if (File.Exists(Path.Combine(wDir, "movie.mp4")) == false)
            {
                throw null;
            }

            Console.WriteLine("*4");             // test
        }
Exemple #15
0
 private void OpenOutput()
 {
     ProcessTools.Batch(new string[] { "start ." }, Ground.OutDir, ProcessTools.WindowStyle_e.INVISIBLE);
 }
Exemple #16
0
        private int VideoStreamIndex     = -1;     // -1 == no video stream

        public FFmpegMediaInfo(string file)
        {
            string[] lines;

            using (WorkingDir wd = new WorkingDir())
            {
                ProcessTools.Batch(new string[]
                {
                    string.Format(@"{0}ffprobe.exe {1} 2> stderr.tmp",
                                  AudioPicMP4Props.FFmpegPathBase,
                                  file
                                  ),
                },
                                   wd.GetPath(".")
                                   );

                string text = File.ReadAllText(wd.GetPath("stderr.tmp"), Encoding.UTF8);

                lines = FileTools.TextToLines(text);
            }

            foreach (string fLine in lines)
            {
                string line = fLine.Trim();

                if (Regex.IsMatch(line, "^Duration: [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2},"))
                {
                    int h = int.Parse(line.Substring(10, 2));
                    int m = int.Parse(line.Substring(13, 2));
                    int s = int.Parse(line.Substring(16, 2));
                    int c = int.Parse(line.Substring(19, 2));

                    int t = h;
                    t *= 60;
                    t += m;
                    t *= 60;
                    t += s;
                    t *= 100;
                    t += c;

                    this.TotalTimeCentisecond = t;
                }
                else if (Regex.IsMatch(line, "^Stream.*Audio:"))
                {
                    string[] tokens  = StringTools.Tokenize(line, StringTools.DECIMAL, true, true);
                    int      strmIdx = int.Parse(tokens[1]);

                    if (strmIdx < 0 || IntTools.IMAX < strmIdx)
                    {
                        throw new Exception("Bad strmIdx: " + strmIdx);
                    }

                    this.AudioStreamIndex = strmIdx;
                }
                else if (Regex.IsMatch(line, "^Stream.*Video:"))
                {
                    string[] tokens  = StringTools.Tokenize(line, StringTools.DECIMAL, true, true);
                    int      strmIdx = int.Parse(tokens[1]);

                    if (strmIdx < 0 || IntTools.IMAX < strmIdx)
                    {
                        throw new Exception("Bad strmIdx: " + strmIdx);
                    }

                    this.VideoStreamIndex = strmIdx;
                }
            }

            if (this.TotalTimeCentisecond < 0 || IntTools.IMAX < this.TotalTimeCentisecond)
            {
                throw new Exception("Bad TotalTimeCentisecond: " + this.TotalTimeCentisecond);
            }
        }