Ejemplo n.º 1
0
        /// <summary>
        /// Apply tag to audio file.
        /// </summary>
        private void tagAudio(UtilsName name, string outName, int episodeCount, int curEpisodeCount, int progessCount,
                              int totalTracks,
                              DateTime filenameStartTime, DateTime filenameEndTime, string lyricSubs1, string lyricSubs2)
        {
            int episodeNum = episodeCount + Settings.Instance.EpisodeStartNumber - 1;

            string tagArtist = name.createName(ConstantSettings.AudioId3Artist, episodeNum,
                                               progessCount, filenameStartTime, filenameEndTime, lyricSubs1, lyricSubs2);

            string tagAlbum = name.createName(ConstantSettings.AudioId3Album, episodeNum,
                                              progessCount, filenameStartTime, filenameEndTime, lyricSubs1, lyricSubs2);

            string tagTitle = name.createName(ConstantSettings.AudioId3Title, episodeNum,
                                              progessCount, filenameStartTime, filenameEndTime, lyricSubs1, lyricSubs2);

            string tagGenre = name.createName(ConstantSettings.AudioId3Genre, episodeNum,
                                              progessCount, filenameStartTime, filenameEndTime, lyricSubs1, lyricSubs2);

            string tagLyrics = name.createName(ConstantSettings.AudioId3Lyrics, episodeNum,
                                               progessCount, filenameStartTime, filenameEndTime, lyricSubs1, lyricSubs2);

            UtilsAudio.tagAudio(outName,
                                tagArtist,
                                tagAlbum,
                                tagTitle,
                                tagGenre,
                                tagLyrics,
                                curEpisodeCount,
                                totalTracks);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Convert audio or video to mp3 and display progress dialog.
        /// </summary>
        private bool convertToMp3(string file, string stream, string progressText, DialogProgress dialogProgress,
                                  DateTime entireClipStartTime, DateTime entireClipEndTime, string tempMp3Filename)
        {
            bool status = true;

            DateTime entireClipDuration = UtilsSubs.getDurationTime(entireClipStartTime, entireClipEndTime);

            DialogProgress.updateProgressInvoke(dialogProgress, progressText);

            // Enable detail mode in progress dialog
            DialogProgress.enableDetailInvoke(dialogProgress, true);

            // Set the duration of the clip in the progress dialog  (for detail mode)
            DialogProgress.setDuration(dialogProgress, entireClipDuration);

            // Rip the audio to a temporary mp3 file
            UtilsAudio.ripAudioFromVideo(file,
                                         stream,
                                         entireClipStartTime, entireClipEndTime,
                                         Settings.Instance.AudioClips.Bitrate, tempMp3Filename, dialogProgress);

            DialogProgress.enableDetailInvoke(dialogProgress, false);

            FileInfo fileInfo = new FileInfo(tempMp3Filename);

            // Error if the temporary mp3 file doesn't exist or is zero bytes
            if (!File.Exists(tempMp3Filename) || fileInfo.Length == 0)
            {
                status = false;
            }

            return(status);
        }
Ejemplo n.º 3
0
        private void backgroundWorkerAudio_DoWork(object sender, DoWorkEventArgs e)
        {
            InfoCombined selectedComb = currentInfoComb;
            DateTime     startTime    = selectedComb.Subs1.StartTime;
            DateTime     endTime      = selectedComb.Subs1.EndTime;
            string       outMp3File   = Path.Combine(previewWorkerVars.MediaDir, ConstantSettings.TempAudioFilename);
            string       outWavFile   = Path.Combine(previewWorkerVars.MediaDir, ConstantSettings.TempAudioPreviewFilename);


            if (audioPreviewNeedsUpdating)
            {
                audioPreviewNeedsUpdating = false;

                if (Settings.Instance.AudioClips.UseAudioFromVideo)
                {
                    if (Settings.Instance.VideoClips.Files.Length > 0)
                    {
                        string videoFileName = Settings.Instance.VideoClips.Files[selectedEpisodeIndex];

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

                        UtilsAudio.ripAudioFromVideo(videoFileName,
                                                     Settings.Instance.VideoClips.AudioStream.Num,
                                                     startTime, endTime,
                                                     Settings.Instance.AudioClips.Bitrate, outMp3File, null);
                    }
                }
                else // Audio file is provided
                {
                    if (Settings.Instance.AudioClips.Files.Length > 0)
                    {
                        string fileToCut      = Settings.Instance.AudioClips.Files[selectedEpisodeIndex];
                        bool   inputFileIsMp3 = (Path.GetExtension(fileToCut).ToLower() == ".mp3");

                        // 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);
                        }

                        if (ConstantSettings.ReencodeBeforeSplittingAudio || !inputFileIsMp3)
                        {
                            UtilsAudio.ripAudioFromVideo(fileToCut,
                                                         "0",
                                                         startTime, endTime,
                                                         Settings.Instance.AudioClips.Bitrate, outMp3File, null);
                        }
                        else
                        {
                            UtilsAudio.cutAudio(fileToCut, startTime, endTime, outMp3File);
                        }
                    }
                }

                // If no external audio player is specified, convert the .mp3 to .wav so that it can be played with SoundPlayer
                if (File.Exists(outMp3File))
                {
                    UtilsAudio.convertAudioFormat(outMp3File, outWavFile, 2);
                }
            }
        }
Ejemplo n.º 4
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);
        }
        /// <summary>
        /// Performs the work of the audio extraction thread.
        /// </summary>
        private void splitAudio(object sender, DoWorkEventArgs e)
        {
            WorkerVars workerVars = e.Argument as WorkerVars;
            List <List <InfoCombined> > combinedAll = new List <List <InfoCombined> >();
            WorkerSubs subsWorker = new WorkerSubs();

            if (groupBoxCheckLyrics.Checked)
            {
                // Parse and combine the subtitles
                try
                {
                    combinedAll = subsWorker.combineAllSubs(workerVars, dialogProgress);

                    if (combinedAll != null)
                    {
                        workerVars.CombinedAll = combinedAll;
                    }
                    else
                    {
                        e.Cancel = true;
                        return;
                    }
                }
                catch (Exception e1)
                {
                    UtilsMsg.showErrMsg("Something went wrong before processing could start.\n" + e1);
                    e.Cancel = true;
                    return;
                }
            }

            DateTime mediaStartime = new DateTime();
            DateTime mediaEndtime  = new DateTime();
            DateTime mediaDuration = new DateTime();

            int episode = 0;

            DialogProgress.updateProgressInvoke(dialogProgress, 0, "Starting...");


            foreach (string file in mediaFiles)
            {
                episode++;

                try
                {
                    mediaEndtime = UtilsVideo.getVideoLength(file);
                }
                catch (Exception e1)
                {
                    UtilsMsg.showErrMsg("Something went wrong while determining duration of the media:\n" + e1);
                    return;
                }

                if (useSpan)
                {
                    mediaStartime = spanStart;

                    // If the span end time if not greater than the actual duration of the media
                    if (spanEnd < mediaEndtime)
                    {
                        mediaEndtime = spanEnd;
                    }
                }

                UtilsName name = new UtilsName(
                    deckName,
                    mediaFiles.Length, // Total number of episodes
                    1,                 // Total number of lines (Note: not filled out here)
                    mediaEndtime,      // Last time
                    0, 0               // Width and height (Note: not filled out anywhere)
                    );

                mediaDuration = UtilsSubs.getDurationTime(mediaStartime, mediaEndtime);

                string progressText = String.Format("Processing audio from media file {0} of {1}",
                                                    episode,
                                                    mediaFiles.Length);

                int progress = Convert.ToInt32((episode - 1) * (100.0 / mediaFiles.Length));

                DialogProgress.updateProgressInvoke(dialogProgress, progress, progressText);

                // Enable detail mode in progress dialog
                DialogProgress.enableDetailInvoke(dialogProgress, true);

                // Set the duration of the clip in the progress dialog  (for detail mode)
                DialogProgress.setDuration(dialogProgress, mediaDuration);


                string tempMp3Filename = Path.GetTempPath() + ConstantSettings.TempAudioFilename;

                UtilsAudio.ripAudioFromVideo(mediaFiles[episode - 1],
                                             audioStream.Num,
                                             mediaStartime, mediaEndtime,
                                             bitrate, tempMp3Filename, dialogProgress);

                DialogProgress.enableDetailInvoke(dialogProgress, false);

                if (dialogProgress.Cancel)
                {
                    e.Cancel = true;
                    File.Delete(tempMp3Filename);
                    return;
                }

                int numClips = 1;

                if (!isSingleFile)
                {
                    numClips = (int)Math.Ceiling((mediaDuration.TimeOfDay.TotalMilliseconds / (clipLength.TimeOfDay.TotalSeconds * 1000.0)));
                }

                for (int clipIdx = 0; clipIdx < numClips; clipIdx++)
                {
                    progressText = String.Format("Splitting segment {0} of {1} from media file {2} of {3}",
                                                 clipIdx + 1,
                                                 numClips,
                                                 episode,
                                                 mediaFiles.Length);

                    progress = Convert.ToInt32((episode - 1) * (100.0 / mediaFiles.Length));

                    DialogProgress.updateProgressInvoke(dialogProgress, progress, progressText);


                    if (dialogProgress.Cancel)
                    {
                        e.Cancel = true;
                        File.Delete(tempMp3Filename);
                        return;
                    }

                    // The start and end times used for processing
                    DateTime startTime = new DateTime();
                    DateTime endTime   = new DateTime();

                    if (isSingleFile)
                    {
                        endTime = mediaDuration;
                    }
                    else
                    {
                        startTime = startTime.AddSeconds((double)(clipLength.TimeOfDay.TotalSeconds * clipIdx));
                        endTime   = endTime.AddSeconds((double)(clipLength.TimeOfDay.TotalSeconds * (clipIdx + 1)));

                        if (endTime.TimeOfDay.TotalMilliseconds >= mediaDuration.TimeOfDay.TotalMilliseconds)
                        {
                            endTime = mediaDuration;
                        }
                    }

                    // The start and end times that will be displayed
                    DateTime startTimeName = startTime.AddMilliseconds(mediaStartime.TimeOfDay.TotalMilliseconds);
                    DateTime endTimeName   = endTime.AddMilliseconds(mediaStartime.TimeOfDay.TotalMilliseconds);

                    // Fill in the total number of lines with the total number of clips
                    name.TotalNumLines = numClips;

                    string nameStr = name.createName(ConstantSettings.ExtractMediaAudioFilenameFormat, episode + episodeStartNumber - 1,
                                                     clipIdx + 1, startTimeName, endTimeName, "", "");


                    string outName = String.Format("{0}{1}{2}",
                                                   outputDir,                   // {0}
                                                   Path.DirectorySeparatorChar, // {1}
                                                   nameStr);                    // {2}

                    UtilsAudio.cutAudio(tempMp3Filename, startTime, endTime, outName);

                    nameStr = name.createName(ConstantSettings.AudioId3Artist, episode + episodeStartNumber - 1,
                                              clipIdx + 1, startTimeName, endTimeName, "", "");

                    string tagArtist = String.Format("{0}",
                                                     nameStr); // {0}

                    nameStr = name.createName(ConstantSettings.AudioId3Album, episode + episodeStartNumber - 1,
                                              clipIdx + 1, startTimeName, endTimeName, "", "");

                    string tagAlbum = String.Format("{0}",
                                                    nameStr); // {0}

                    nameStr = name.createName(ConstantSettings.AudioId3Title, episode + episodeStartNumber - 1,
                                              clipIdx + 1, startTimeName, endTimeName, "", "");

                    string tagTitle = String.Format("{0}",
                                                    nameStr); // {0}

                    nameStr = name.createName(ConstantSettings.AudioId3Genre, episode + episodeStartNumber - 1,
                                              clipIdx + 1, startTimeName, endTimeName, "", "");

                    string tagGenre = String.Format("{0}",
                                                    nameStr); // {0}

                    string tagLyrics = "";

                    if (groupBoxCheckLyrics.Checked)
                    {
                        int totalLyricsLines = 0;
                        int curLyricsNum     = 1;

                        // Precount the number of lyrics lines
                        foreach (InfoCombined comb in combinedAll[episode - 1])
                        {
                            if (comb.Subs1.StartTime.TimeOfDay.TotalMilliseconds >= startTimeName.TimeOfDay.TotalMilliseconds &&
                                comb.Subs1.StartTime.TimeOfDay.TotalMilliseconds <= endTimeName.TimeOfDay.TotalMilliseconds)
                            {
                                totalLyricsLines++;
                            }
                        }

                        // Fill in the total number of lyrics lines
                        name.TotalNumLines = curLyricsNum;

                        // Foreach comb in the current episode, if the comb lies within the
                        // current clip, add it to the lryics tag
                        foreach (InfoCombined comb in combinedAll[episode - 1])
                        {
                            if (comb.Subs1.StartTime.TimeOfDay.TotalMilliseconds >= startTimeName.TimeOfDay.TotalMilliseconds &&
                                comb.Subs1.StartTime.TimeOfDay.TotalMilliseconds <= endTimeName.TimeOfDay.TotalMilliseconds)
                            {
                                tagLyrics += formatLyricsPair(comb, name, startTimeName, episode + episodeStartNumber - 1, curLyricsNum) + "\r\n";
                                curLyricsNum++;
                            }
                        }
                    }

                    UtilsAudio.tagAudio(outName,
                                        tagArtist,
                                        tagAlbum,
                                        tagTitle,
                                        tagGenre,
                                        tagLyrics,
                                        clipIdx + 1,
                                        numClips);
                }
            }

            return;
        }