Beispiel #1
0
        /// <summary>
        /// menuEdit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuCut_Click(object sender, EventArgs e)
        {
            using (frmGetTime getTime = new frmGetTime())
            {
                if (getTime.ShowDialog() == DialogResult.OK)
                {
                    using (SaveFileDialog save = new SaveFileDialog()
                    {
                        Filter = "(mp3 file)|*.mp3"
                    })
                    {
                        if (save.ShowDialog() == DialogResult.OK)
                        {
                            // Lay ra startTime va durationTime cho Argument cua processStartInfo
                            int startTime    = 0;
                            int durationTime = 0;

                            cbxSpeed.Text = "hh:mm:ss";

                            // Mode hh:mm:ss
                            startTime    = regex.getTime("0", getTime.txtTime1.Text);
                            durationTime = regex.getTime(getTime.txtTime1.Text, getTime.txtTime2.Text);

                            // -i input.mp3 -ss 00:03:00 -t 60 -c copy output.mp3
                            // -i input.mp3 -ss 3:00 -t 60 -c copy output.mp3
                            // -i input.mp3 -ss 180 -t 60 -c copy output.mp3
                            processStartInfo.FileName  = "ffmpeg";
                            processStartInfo.Arguments = "-i " + open.FileName + " -ss " + startTime + " -t " + durationTime + " " + save.FileName;

                            using (Process process = new Process()
                            {
                                StartInfo = processStartInfo
                            })
                            {
                                if (process.Start())
                                {
                                    MessageBox.Show("Excuting", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    process.WaitForExit();
                                }
                            }
                        }
                    }
                }
            }
        }
        private void menuSplit_Click(object sender, EventArgs e)
        {
            try
            {
                // index == 6
                index = ((ToolStripMenuItem)sender).Owner.Items.IndexOf((ToolStripMenuItem)sender);

                cbxSpecies.Items.Clear();
                cbxSpecies.Items.Add("hh:mm:ss");
                cbxSpecies.Items.Add("ss");

                if (string.IsNullOrEmpty(open.FileName))
                {
                    MessageBox.Show("Your path is incorrect", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    // Trong cbxSpecies cbxSpecies.Text khong phai la 1 Items nen co the kiem tra Text vs cac Items cua no
                    for (int i = 0; i < cbxSpecies.Items.Count; i++)
                    {
                        if (cbxSpecies.Text == cbxSpecies.Items[i].ToString())
                        {
                            break;
                        }
                        else if (cbxSpecies.Text != cbxSpecies.Items[i].ToString() && i == cbxSpecies.Items.Count - 1)
                        {
                            cbxSpecies.Text = "";
                        }
                    }

                    if (!string.IsNullOrEmpty(cbxSpecies.Text))
                    {
                        txtFileName.Text = open.FileName;

                        using (frmGetTime getTime = new frmGetTime())
                        {
                            if (getTime.ShowDialog() == DialogResult.OK)
                            {
                                //// Cach 1: Su dung FolderBrownser
                                //using (FolderBrowserDialog folderBrowser = new FolderBrowserDialog())
                                //{
                                //    if (folderBrowser.ShowDialog() == DialogResult.OK)
                                //    {
                                //        int durationTime = 0;

                                //        if (cbxSpecies.Text == cbxSpecies.Items[0].ToString())
                                //        {
                                //            durationTime = regex.getTime("0", getTime.txtTime1.Text);
                                //        }
                                //        else
                                //        {
                                //            durationTime = int.Parse(getTime.txtTime1.Text);
                                //        }

                                //        // -i input.mp4 -c copy -f segment -segment_time 40 -reset_timestamps 1 %d.mp4
                                //        processStartInfo.FileName = "ffmpeg";
                                //        processStartInfo.Arguments = "-i " + open.FileName + " -c copy -f segment -segment_time " + durationTime + " -reset_timestamps 1 " + folderBrowser.SelectedPath + "\\0%d.mp4";

                                //        txtFileName.Text = processStartInfo.Arguments;

                                //        using (Process process = new Process() { StartInfo = processStartInfo })
                                //        {
                                //            if (process.Start())
                                //            {
                                //                MessageBox.Show("Excuting", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                //            }
                                //            process.Close();
                                //        }
                                //    }
                                //}

                                // Cach 2: Su dung SaveFileDialog
                                if (save.ShowDialog() == DialogResult.OK)
                                {
                                    string path = regex.getSplitFileName(save.FileName, "mp4");

                                    int durationTime = 0;
                                    if (cbxSpecies.Text == cbxSpecies.Items[0].ToString())
                                    {
                                        durationTime = regex.getTime("0", getTime.txtTime1.Text);
                                    }
                                    else
                                    {
                                        durationTime = int.Parse(getTime.txtTime1.Text);
                                    }

                                    // -i input.mp4 - c copy - f segment - segment_time 40 - reset_timestamps 1 %d.mp4

                                    /// ===> Tao ra N video la %d.mp4 tu video cu la input.mp4, trong do:
                                    ///         So luong N phu thuoc vao do dai goc cua video cu input.mp4 va do dai moi video moi va do dai moi video moi la 40 giay
                                    ///         %d co tac lam cho N video sau khi luu se co ten tu dong tang dan o vi tri %d la 1, 2, 3, ... N => cau lenh moi chay dc
                                    /*Neu tao ra N video ma ta chi luu 1, 2 video < N thi tat ca video se tu dong khong luu nua*/
                                    processStartInfo.FileName  = "ffmpeg";
                                    processStartInfo.Arguments = "-i " + open.FileName + " -c copy -f segment -segment_time " + durationTime + " -reset_timestamps 1 " + path;

                                    using (Process process = new Process()
                                    {
                                        StartInfo = processStartInfo
                                    })
                                    {
                                        if (process.Start())
                                        {
                                            MessageBox.Show("Excuting", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                            process.WaitForExit();
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        txtFileName.Text = "Select videos splitting mode";
                    }
                }
            }
            catch (MyException myEx)
            {
                MessageBox.Show(myEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void menuTrim_Click(object sender, EventArgs e)
        {
            try
            {
                // index == 4
                index = ((ToolStripMenuItem)sender).Owner.Items.IndexOf((ToolStripMenuItem)sender);

                cbxSpecies.Items.Clear();
                cbxSpecies.Items.Add("hh:mm:ss");
                cbxSpecies.Items.Add("ss");

                if (string.IsNullOrEmpty(open.FileName))
                {
                    MessageBox.Show("Your path is incorrect", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    // Trong cbxSpecies cbxSpecies.Text khong phai la 1 Items nen co the kiem tra Text vs cac Items cua no
                    for (int i = 0; i < cbxSpecies.Items.Count; i++)
                    {
                        if (cbxSpecies.Text == cbxSpecies.Items[i].ToString())
                        {
                            break;
                        }
                        else if (cbxSpecies.Text != cbxSpecies.Items[i].ToString() && i == cbxSpecies.Items.Count - 1)
                        {
                            cbxSpecies.Text = "";
                        }
                    }

                    if (!string.IsNullOrEmpty(cbxSpecies.Text))
                    {
                        txtFileName.Text = open.FileName;

                        using (frmGetTime getTime = new frmGetTime())
                        {
                            if (getTime.ShowDialog() == DialogResult.OK)
                            {
                                if (save.ShowDialog() == DialogResult.OK)
                                {
                                    // Lay ra FileName cua SaveFileDialog va them so sau ten de dat ten 2 file moi
                                    List <string> listResult = regex.getFileName(save.FileName);

                                    // Lay ra startTime va durationTime cho Argument cua processStartInfo
                                    int durationTime = regex.getTime("0", getTime.txtTime1.Text);
                                    int startTime    = 0;

                                    // Mode hh:mm:ss
                                    if (cbxSpecies.Text == cbxSpecies.Items[0].ToString())
                                    {
                                        startTime = regex.getTime("0", getTime.txtTime2.Text);
                                    }
                                    // Mode ss
                                    else
                                    {
                                        startTime = int.Parse(getTime.txtTime1.Text);
                                    }

                                    // -i input.mp4 -t 60 -c copy part1.mp4 -ss 00:01:30 -codec copy part2.mp4

                                    /// ===> Tao ra 2 video moi la part1.mp4 va part2.mp4 tu video cu la input.mp4, trong do:
                                    ///         video part1.mp4 se luon luon bat dau tu 00:00:00 va keo dai trong 60 giay
                                    ///         video part2.mp4 se luon luon bat dau tu 00:01:30 va keo dai den het video cu input.mp4
                                    processStartInfo.FileName  = "ffmpeg";
                                    processStartInfo.Arguments = "-i " + open.FileName + " -t " + durationTime + " -c copy " + listResult[0] + " -ss " + startTime + " -codec copy " + listResult[1];

                                    using (Process process = new Process()
                                    {
                                        StartInfo = processStartInfo
                                    })
                                    {
                                        if (process.Start())
                                        {
                                            MessageBox.Show("Excuting", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                            process.WaitForExit();
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        txtFileName.Text = "Select videos trimming mode";
                    }
                }
            }
            catch (MyException myEx)
            {
                MessageBox.Show(myEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void menuCut_Click(object sender, EventArgs e)
        {
            try
            {
                // index == 2
                index = ((ToolStripMenuItem)sender).Owner.Items.IndexOf((ToolStripMenuItem)sender);

                cbxSpecies.Items.Clear();
                cbxSpecies.Items.Add("hh:mm:ss");
                cbxSpecies.Items.Add("ss");

                if (string.IsNullOrEmpty(open.FileName))
                {
                    MessageBox.Show("Your path is incorrect", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    // Trong cbxSpecies cbxSpecies.Text khong phai la 1 Items nen co the kiem tra Text vs cac Items cua no
                    for (int i = 0; i < cbxSpecies.Items.Count; i++)
                    {
                        if (cbxSpecies.Text == cbxSpecies.Items[i].ToString())
                        {
                            break;
                        }
                        else if (cbxSpecies.Text != cbxSpecies.Items[i].ToString() && i == cbxSpecies.Items.Count - 1)
                        {
                            cbxSpecies.Text = "";
                        }
                    }

                    if (!string.IsNullOrEmpty(cbxSpecies.Text))
                    {
                        txtFileName.Text = open.FileName;

                        using (frmGetTime getTime = new frmGetTime())
                        {
                            // Do ta su dung 1 Form khac de tra ve gia tri durationTime nen phai de phong TH users khong an OK ma an huy
                            if (getTime.ShowDialog() == DialogResult.OK)
                            {
                                if (save.ShowDialog() == DialogResult.OK)
                                {
                                    // Lay ra startTime va durationTime cho Argument cua processStartInfo
                                    int startTime    = 0;
                                    int durationTime = 0;

                                    // Mode hh:mm:ss
                                    if (cbxSpecies.Text == cbxSpecies.Items[0].ToString())
                                    {
                                        startTime    = regex.getTime("0", getTime.txtTime1.Text);
                                        durationTime = regex.getTime(getTime.txtTime1.Text, getTime.txtTime2.Text);
                                    }
                                    // Mode ss
                                    else
                                    {
                                        startTime    = 0;
                                        durationTime = int.Parse(getTime.txtTime1.Text);
                                    }

                                    // Cach 1:
                                    // -i input.mp4 -ss 00:00:03 -t 00:01:18 -async 1 output.mp4
                                    // -i input.mp4 -ss 00:00:03 -t 1:18 -async 1 output.mp4
                                    // -i input.mp4 -ss 00:00:03 -t 78 -async 1 output.mp4
                                    // --> 3 cau lenh tren deu giong nhau het

                                    // Cach 2:
                                    // -i movie.mp4 -ss 00:00:03 -t 00:00:08 -async 1 -strict -2 cut.mp4

                                    // cach 3: It dung hon
                                    // -i input.mp4 -t 50 output.mp4

                                    /// ===> Tao ra 1 video moi output.mp4 tu video cu input.mp4 nhung co timeStart tu 00:00:03 va keo dai trong durationTime = 78 giay
                                    processStartInfo.FileName  = "ffmpeg";
                                    processStartInfo.Arguments = "-i " + open.FileName + " -ss " + startTime + " -t " + durationTime + " -async 1 " + save.FileName;
                                    // -i C:\Users\Admin\Desktop\abc.mp4 -ss 60 -t 60 -async 1 C:\Users\Admin\Desktop\testCutVideosHHMMSS.mp4
                                    // -i C:\Users\Admin\Desktop\abc.mp4 -ss 0 -t 40 -async 1 C:\Users\Admin\Desktop\testCutVideosSS.mp4

                                    using (Process process = new Process()
                                    {
                                        StartInfo = processStartInfo
                                    })
                                    {
                                        if (process.Start())
                                        {
                                            MessageBox.Show("Excuting", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                            process.WaitForExit();
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        txtFileName.Text = "Select videos cutting mode";
                    }
                }
            }
            catch (MyException myEx)
            {
                MessageBox.Show(myEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }