Ejemplo n.º 1
0
        private bool FixAudioDelay()
        {
            string encoderParams;

            _jobStatus.PercentageComplete = 100; //all good to start with
            _jobStatus.ETA = "";

            if (_videoFile.AudioDelaySet || _toolAudioDelay == 0)
                return true; //It's already been done (probably by mencoder) or been requested to skip

            // Check if the converted file has Audio AND Video streams (if one is missing, then skip this step)
            FFmpegMediaInfo ffmpegInfo = new FFmpegMediaInfo(_convertedFile, _jobStatus, _jobLog);
            if (!ffmpegInfo.Success || ffmpegInfo.ParseError)
            {
                _jobStatus.PercentageComplete = 0; // if the file wasn't completely converted the percentage will be low so no worries
                _jobStatus.ErrorMsg = "Fix AudioSync getting mediainfo Failed for " + _convertedFile;
                _jobLog.WriteEntry(this, (_jobStatus.ErrorMsg), Log.LogEntryType.Error);
                return false;
            }

            if ((ffmpegInfo.MediaInfo.VideoInfo.Stream == -1) || (ffmpegInfo.AudioTracks < 1))
            {
                _jobLog.WriteEntry(this, "Fix audiosync, No video or no audio track detected - skipping audio sync", Log.LogEntryType.Warning);
                return true;
            }

            double audioDelay = _toolAudioDelay;

            if (audioDelay != 0)
            {
                _jobLog.WriteEntry(this, ("Fixing Audio Delay, Detected :") + " " + _videoFile.AudioDelay.ToString(System.Globalization.CultureInfo.InvariantCulture) + ", Manual Adj : " + _toolAudioDelay.ToString(System.Globalization.CultureInfo.InvariantCulture), Log.LogEntryType.Information);

                string ext = FilePaths.CleanExt(_convertedFile);
                string fixedFile = Path.Combine(_workingPath, Path.GetFileNameWithoutExtension(_convertedFile) + "_AVFIX" + FilePaths.CleanExt(_convertedFile));
                FileIO.TryFileDelete(fixedFile);

                _jobStatus.CurrentAction = Localise.GetPhrase("Correcting audio delay");

                switch (ext)
                {
                    case ".wmv":
                        _jobLog.WriteEntry(this, ("Using ASFBin to correct audio sync for extension ") + ext, Log.LogEntryType.Debug);

                        encoderParams = " -i " + Util.FilePaths.FixSpaces(_convertedFile) + " -adelay " + audioDelay.ToString(System.Globalization.CultureInfo.InvariantCulture) + " -o " + Util.FilePaths.FixSpaces(fixedFile) + " -y";
                        ASFBin asfBin = new ASFBin(encoderParams, _jobStatus, _jobLog);
                        asfBin.Run();
                        if (!asfBin.Success || (FileIO.FileSize(fixedFile) <= 0))
                        {
                            _jobStatus.ErrorMsg = "Fixing Audio Delay for WMV failed";
                            _jobLog.WriteEntry(this, ("Fixing Audio Delay for WMV failed"), Log.LogEntryType.Error);
                            _jobStatus.PercentageComplete = 0;
                            return false;
                        }

                        break;

                    case ".avi":
                        _jobLog.WriteEntry(this, ("Using Mencoder to correct audio sync for extension ") + ext, Log.LogEntryType.Debug);

                        encoderParams = Util.FilePaths.FixSpaces(_convertedFile) + " -oac copy -ovc copy -ni -delay " + (-1 * audioDelay).ToString(System.Globalization.CultureInfo.InvariantCulture) + " -o " + Util.FilePaths.FixSpaces(fixedFile.ToString(System.Globalization.CultureInfo.InvariantCulture)); // avoid using threads since we are copying to increase stability

                        _jobLog.WriteEntry(this, "Fixing Audio Delay using MEncoder with Parameters: " + encoderParams, Log.LogEntryType.Debug);
                        Mencoder mencoderAVI = new Mencoder(encoderParams, _jobStatus, _jobLog, false);
                        mencoderAVI.Run();
                        if (!mencoderAVI.Success) // something failed or was incomplete, do not check for % completion as Mencoder looks fro success criteria
                        {
                            _jobStatus.PercentageComplete = 0;
                            _jobStatus.ErrorMsg = ("Fix AudioSync failed for") + " " + ext;
                            _jobLog.WriteEntry(this, (_jobStatus.ErrorMsg), Log.LogEntryType.Error);
                            return false;
                        }

                        break;

                    default:
                        _jobLog.WriteEntry(this, ("Using FFMPEG to correct audio sync for extension ") + ext, Log.LogEntryType.Debug);

                        if (audioDelay > 0) // Map same file as 2 inputs, shift and take the audio in one and take the video from the other
                        {
                            encoderParams = "-y -i " + Util.FilePaths.FixSpaces(_convertedFile) +
                                            " -ss " + audioDelay.ToString(System.Globalization.CultureInfo.InvariantCulture) +" -i " + Util.FilePaths.FixSpaces(_convertedFile) +
                                            " -map 1:v -map 0:a -acodec copy -vcodec copy";

                            _jobLog.WriteEntry(this, "Fixing +ve Audio Delay using FFMPEG", Log.LogEntryType.Debug);

                        } // if audio is behind the video skip seconds from the 2nd input file and remap to ouput (keeping the audio shift positive)
                        else
                        {
                            encoderParams = "-y -ss " + (audioDelay * -1).ToString(System.Globalization.CultureInfo.InvariantCulture) + " -i " + Util.FilePaths.FixSpaces(_convertedFile) +
                                            " -i " + Util.FilePaths.FixSpaces(_convertedFile) +
                                            " -map 1:v -map 0:a -acodec copy -vcodec copy";

                            _jobLog.WriteEntry(this, "Fixing -ve Audio Delay using FFMPEG", Log.LogEntryType.Debug);
                        }

                        encoderParams += " " + Util.FilePaths.FixSpaces(fixedFile);

                        if (!FFmpeg.FFMpegExecuteAndHandleErrors(encoderParams, _jobStatus, _jobLog, Util.FilePaths.FixSpaces(fixedFile))) // Do not check for % completion since FFMPEG doesn't always report a % for this routine for some reason
                        {
                            _jobStatus.PercentageComplete = 0; // if the file wasn't completely converted the percentage will be low so no worries
                            _jobStatus.ErrorMsg = "Fix AudioSync Failed for " + ext;
                            _jobLog.WriteEntry(this, (_jobStatus.ErrorMsg), Log.LogEntryType.Error);
                            return false;
                        }

                        break;
                }
                
                try
                {
                    _jobLog.WriteEntry(this, ("Fix Audio Delay trying to move fixed file"), Log.LogEntryType.Information);
                    FileIO.TryFileDelete(_convertedFile);
                    File.Move(fixedFile, _convertedFile);
                }
                catch (Exception e)
                {
                    _jobLog.WriteEntry(this, ("Unable to move audio sync corrected file") + " " + fixedFile + "\r\nError : " + e.ToString(), Log.LogEntryType.Error);
                    _jobStatus.ErrorMsg = "Unable to move audio sync file";
                    _jobStatus.PercentageComplete = 0;
                    return false;
                }
                _jobLog.WriteEntry(this, ("Finished Audio Delay Correction, file size [KB]") + " " + (FileIO.FileSize(_convertedFile) / 1024).ToString("N", System.Globalization.CultureInfo.InvariantCulture), Log.LogEntryType.Debug);
            }
            else
                _jobLog.WriteEntry(this, ("Fix Audio Delay, net correction 0, skipping correction"), Log.LogEntryType.Information);

            return true;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Remove commercials from WMV files using ASFBin
        /// </summary>
        private void CutWMV()
        {
            List<KeyValuePair<float, float>> keepList = new List<KeyValuePair<float, float>>();

            // Read the EDL file and convert into MP4Box cut structure
            if (!ParseEDLFile(ref keepList))
                return;

            // Create params to cut the file
            string tempFile = CutFileName(_uncutVideo, 0, 0); // temp cut file
            int cutNumber = 0;
            string cutParams = " -i " + Util.FilePaths.FixSpaces(_uncutVideo);

            foreach (KeyValuePair<float, float> keep in keepList)
            {
                cutParams += " -start " + keep.Key.ToString(CultureInfo.InvariantCulture) + " -dur " + (keep.Value - keep.Key).ToString(CultureInfo.InvariantCulture);
                cutNumber++;
            }

            cutParams += " -o " + Util.FilePaths.FixSpaces(tempFile) + " -y";

            if (cutNumber < 1)
            {
                _jobLog.WriteEntry(this, Localise.GetPhrase("No commercials to remove from ") + EDLFile, Log.LogEntryType.Information);
                _jobStatus.PercentageComplete = 100; //Set to success since sometime mp4box doesn't set to 100 if there no/incomplete pieces to strip
                return;
            }

            // Cut the file
            _jobStatus.CurrentAction = Localise.GetPhrase("Merging commercial free segments into new video");
            _jobLog.WriteEntry(this, Localise.GetPhrase("CutWMV: Merging commercial free segments into new video"), Log.LogEntryType.Information);
            
            ASFBin asfBin= new ASFBin(cutParams, _jobStatus, _jobLog);
            asfBin.Run();
            if (!asfBin.Success || (Util.FileIO.FileSize(tempFile) <= 0))
            {
                _jobStatus.ErrorMsg = "CutWMV Commercial cutting failed";
                _jobLog.WriteEntry(this, Localise.GetPhrase("CutWMV Commercial cutting failed"), Log.LogEntryType.Error);
                _jobStatus.PercentageComplete = 0;
                return;
            }

            _jobLog.WriteEntry(this, Localise.GetPhrase("CutWMV trying to replace file") + " Output : " + _uncutVideo + " Temp : " + tempFile, Log.LogEntryType.Debug);
            RenameAndMoveFile(tempFile);
        }