Beispiel #1
0
        /// <summary>
        /// Generate Audio clips for all episodes.
        /// </summary>
        public bool genAudioClip(WorkerVars workerVars, DialogProgress dialogProgress)
        {
            int      progessCount    = 0;
            int      episodeCount    = 0;
            int      totalEpisodes   = workerVars.CombinedAll.Count;
            int      curEpisodeCount = 0;
            int      totalLines      = UtilsSubs.getTotalLineCount(workerVars.CombinedAll);
            DateTime lastTime        = UtilsSubs.getLastTime(workerVars.CombinedAll);

            UtilsName name = new UtilsName(Settings.Instance.DeckName, totalEpisodes,
                                           totalLines, lastTime, Settings.Instance.VideoClips.Size.Width,
                                           Settings.Instance.VideoClips.Size.Height);

            DialogProgress.updateProgressInvoke(dialogProgress, 0, "Creating audio clips.");

            // For each episode
            foreach (List <InfoCombined> combArray in workerVars.CombinedAll)
            {
                episodeCount++;

                // It is possible for all lines in an episode to be set to inactive
                if (combArray.Count == 0)
                {
                    // Skip this episode
                    continue;
                }

                // Is the audio input an mp3 file?
                bool inputFileIsMp3 = Settings.Instance.AudioClips.Files.Length > 0 &&
                                      Path.GetExtension(Settings.Instance.AudioClips.Files[episodeCount - 1])
                                      .ToLower() == ".mp3";

                DateTime entireClipStartTime = combArray[0].Subs1.StartTime;
                DateTime entireClipEndTime   = combArray[combArray.Count - 1].Subs1.EndTime;
                string   tempMp3Filename     = Path.GetTempPath() + ConstantSettings.TempAudioFilename;

                // Apply pad to entire clip timings  (if requested)
                if (Settings.Instance.AudioClips.PadEnabled)
                {
                    entireClipStartTime =
                        UtilsSubs.applyTimePad(entireClipStartTime, -Settings.Instance.AudioClips.PadStart);
                    entireClipEndTime = UtilsSubs.applyTimePad(entireClipEndTime, Settings.Instance.AudioClips.PadEnd);
                }

                // Do we need to extract the audio from the video file?
                if (Settings.Instance.AudioClips.UseAudioFromVideo)
                {
                    string progressText = $"Extracting audio from video file {episodeCount} of {totalEpisodes}"; // {1}

                    bool success = convertToMp3(
                        Settings.Instance.VideoClips.Files[episodeCount - 1],
                        Settings.Instance.VideoClips.AudioStream.Num,
                        progressText,
                        dialogProgress,
                        entireClipStartTime,
                        entireClipEndTime,
                        tempMp3Filename);

                    if (!success)
                    {
                        UtilsMsg.showErrMsg("Failed to extract the audio from the video.\n" +
                                            "Make sure that the video does not have any DRM restrictions.");
                        return(false);
                    }
                }
                // If the reencode option is set or the input audio is not an mp3, reencode to mp3
                else if (ConstantSettings.ReencodeBeforeSplittingAudio || !inputFileIsMp3)
                {
                    string progressText = $"Reencoding audio file {episodeCount} of {totalEpisodes}";

                    bool success = convertToMp3(
                        Settings.Instance.AudioClips.Files[episodeCount - 1],
                        "0",
                        progressText,
                        dialogProgress,
                        entireClipStartTime,
                        entireClipEndTime,
                        tempMp3Filename);

                    if (!success)
                    {
                        UtilsMsg.showErrMsg("Failed to reencode the audio file.\n" +
                                            "Make sure that the audio file does not have any DRM restrictions.");
                        return(false);
                    }
                }

                curEpisodeCount = 0; // Reset

                // For each line in episode, generate an audio clip
                foreach (InfoCombined comb in combArray)
                {
                    progessCount++;
                    curEpisodeCount++;

                    int progress = Convert.ToInt32(progessCount * (100.0 / totalLines));

                    string progressText =
                        $"Generating audio clip: {progessCount.ToString()} of {totalLines.ToString()}";

                    // Update the progress dialog
                    DialogProgress.updateProgressInvoke(dialogProgress, progress, progressText);

                    // Did the user press the cancel button?
                    if (dialogProgress.Cancel)
                    {
                        File.Delete(tempMp3Filename);
                        return(false);
                    }

                    DateTime startTime         = comb.Subs1.StartTime;
                    DateTime endTime           = comb.Subs1.EndTime;
                    DateTime filenameStartTime = comb.Subs1.StartTime;
                    DateTime filenameEndTime   = comb.Subs1.EndTime;
                    string   fileToCut         = "";

                    if (Settings.Instance.AudioClips.UseAudioFromVideo ||
                        ConstantSettings.ReencodeBeforeSplittingAudio || !inputFileIsMp3)
                    {
                        startTime = UtilsSubs.shiftTiming(startTime,
                                                          -(int)entireClipStartTime.TimeOfDay.TotalMilliseconds);
                        endTime = UtilsSubs.shiftTiming(endTime,
                                                        -(int)entireClipStartTime.TimeOfDay.TotalMilliseconds);
                        fileToCut = tempMp3Filename;
                    }
                    else
                    {
                        fileToCut = Settings.Instance.AudioClips.Files[episodeCount - 1];
                    }

                    // Apply pad (if requested)
                    if (Settings.Instance.AudioClips.PadEnabled)
                    {
                        startTime         = UtilsSubs.applyTimePad(startTime, -Settings.Instance.AudioClips.PadStart);
                        endTime           = UtilsSubs.applyTimePad(endTime, Settings.Instance.AudioClips.PadEnd);
                        filenameStartTime = UtilsSubs.applyTimePad(comb.Subs1.StartTime,
                                                                   -Settings.Instance.AudioClips.PadStart);
                        filenameEndTime =
                            UtilsSubs.applyTimePad(comb.Subs1.EndTime, Settings.Instance.AudioClips.PadEnd);
                    }

                    string lyricSubs2 = "";

                    // Set the Subs2 lyric if it exists
                    if (Settings.Instance.Subs[1].Files.Length != 0)
                    {
                        lyricSubs2 = comb.Subs2.Text.Trim();
                    }

                    // Create output filename
                    string nameStr = name.createName(ConstantSettings.AudioFilenameFormat,
                                                     (int)episodeCount + Settings.Instance.EpisodeStartNumber - 1,
                                                     progessCount, filenameStartTime, filenameEndTime, comb.Subs1.Text, lyricSubs2);

                    string outName = $"{workerVars.MediaDir}{Path.DirectorySeparatorChar}{nameStr}"; // {2}

                    // Create audio clip
                    UtilsAudio.cutAudio(fileToCut, startTime, endTime, outName);

                    // Tag the audio clip
                    tagAudio(name, outName, episodeCount, curEpisodeCount, progessCount, combArray.Count,
                             filenameStartTime, filenameEndTime, comb.Subs1.Text, lyricSubs2);
                }

                File.Delete(tempMp3Filename);
            }

            // Normalize all mp3 files in the media directory
            if (Settings.Instance.AudioClips.Normalize)
            {
                DialogProgress.updateProgressInvoke(dialogProgress, -1, "Normalizing audio...");

                UtilsAudio.normalizeAudio(workerVars.MediaDir);
            }

            return(true);
        }