// DAR = Display Aspect Ratio
        // PAR = Pixel Aspect Ratio
        // RAR = Resolution Aspect Ratio

        // D = R * P
        // P = D / R
        // R = D / P

        private MPEG4PAR GetMPEG4PARFromForm()
        {
            RadioButton rb;
            string      tag, val;
            MPEG4PAR    mp4PAR = new MPEG4PAR();

            foreach (Control c in grpAspectRatio.Controls)
            {
                if ((rb = c as RadioButton) != null)
                {
                    if (rb.Checked)
                    {
                        tag = (string)rb.Tag;
                        val = tag.Substring(4);
                        if (tag.StartsWith("PAR"))
                        {
                            // PAR
                            if (val == "Custom")
                            {
                                double par = Double.Parse(txtPAR_Width.Text) /
                                             Double.Parse(txtPAR_Height.Text);
                                mp4PAR.SetCustomPAR(par);
                            }
                            else
                            {
                                mp4PAR.Type = (PARType)UInt32.Parse(val);
                            }
                        }
                        else
                        {
                            // DAR
                            double dar, rar, par;
                            if (val == "Custom")
                            {
                                dar = Double.Parse(txtDAR_Width.Text) /
                                      Double.Parse(txtDAR_Height.Text);
                            }
                            else
                            {
                                string[] split = val.Split(':');
                                dar = Double.Parse(split[0]) / Double.Parse(split[1]);
                            }
                            rar = (double)_mp4Mod.FrameWidth / (double)_mp4Mod.FrameHeight;
                            par = dar / rar;
                            mp4PAR.SetCustomPAR(par);
                        }
                        break;
                    }
                }
            }

            return(mp4PAR);
        }
示例#2
0
        private static int Run()
        {
            if (!File.Exists(_pathSrc))
            {
                throw new Exception("Source file doesn't exist.");
            }
            if ((_pathDst != null) && File.Exists(_pathDst))
            {
                throw new Exception("Destination file already exists.");
            }

            bool modifying = false;

            _loading = true;
            AVIModifier        aviMod = new AVIModifier(_pathSrc);
            MPEG4FrameModifier mp4Mod = new MPEG4FrameModifier();

            aviMod.FrameModifier    = mp4Mod;
            mp4Mod.VideoModifier    = aviMod;
            aviMod.ProgressCallback = new ProgressCallback(Progress);
            aviMod.Preview();
            Progress(1.0);
            Console.WriteLine();

            if (_showInfo)
            {
                Console.WriteLine("---------- Video Info ----------");
                Console.Write(mp4Mod.GenerateStats());
                Console.WriteLine("--------------------------------");
            }

            if (_writeFrameList)
            {
                Console.Write("Writing frame list...");
                try {
                    FileStream fs = new FileStream(_frameListPath, FileMode.CreateNew, FileAccess.Write);
                    using (StreamWriter sw = new StreamWriter(fs)) {
                        mp4Mod.DumpFrameList(sw);
                    }
                }
                catch {
                    Console.WriteLine();
                    throw new Exception("Cannot write frame list, make sure the file doesn't already exist.");
                }
                Console.WriteLine(" Done.");
            }

            if (_pathDst == null)
            {
                return(0);
            }

            if (_unpack && mp4Mod.IsPacked)
            {
                modifying           = true;
                mp4Mod.NewIsPacked  = false;
                mp4Mod.UserDataList = mp4Mod.SuggestedUserData();
            }
            if (_pack && !mp4Mod.IsPacked && mp4Mod.ContainsBVOPs)
            {
                modifying           = true;
                mp4Mod.NewIsPacked  = true;
                mp4Mod.UserDataList = mp4Mod.SuggestedUserData();
            }
            if (_changePAR || _changeDAR)
            {
                MPEG4PAR pold = mp4Mod.PARInfo;
                MPEG4PAR pnew = new MPEG4PAR();

                if (_changePAR)
                {
                    if (_parType != PARType.Custom)
                    {
                        pnew.Type = _parType;
                    }
                    else
                    {
                        pnew.SetCustomPAR(_customPAR);
                    }
                }
                else if (_changeDAR)
                {
                    double rar = (double)mp4Mod.FrameWidth / (double)mp4Mod.FrameHeight;
                    pnew.SetCustomPAR(_customDAR / rar);
                }

                if ((pold.Type != pnew.Type) ||
                    ((pnew.Type == PARType.Custom) &&
                     ((pnew.Width != pold.Width) || (pnew.Height != pold.Height))))
                {
                    modifying      = true;
                    mp4Mod.PARInfo = pnew;
                }
            }
            if (_changeFieldOrder && mp4Mod.IsInterlaced && (mp4Mod.TopFieldFirst != _isTFF))
            {
                modifying            = true;
                mp4Mod.TopFieldFirst = _isTFF;
            }

            if (!modifying && !_alwaysWrite)
            {
                Console.WriteLine("Aborting: Video already has the desired format.");
                return(2);
            }

            _loading = false;
            aviMod.Write(_pathDst);
            Progress(1.0);
            Console.WriteLine();

            Console.WriteLine("Video was written successfully.");
            return(0);
        }