コード例 #1
0
ファイル: Program.cs プロジェクト: HAN-IBS/ffmpeg4tc
 static void xEnc(string[] fnl)
 {
     for (int f = 0; f < fnl.Length; f++)
     {
         string fni = fnl[f]; fn = fni;
         fdI = Path.GetDirectoryName(fni) + "\\"; fdO = fdI;
         if (fni.IndexOf("\\") != -1)
         {
             fn = Path.GetFileName(fni);
         }
         if (!File.Exists(fdI + fn))
         {
             continue;
         }
         long   fz = new FileInfo(fdI + fn).Length;
         string dn = fdO + fn.Replace(Path.GetExtension(fn), "_out." + fmt);
         int    x  = f > 1?".mp3.aac.m4a".IndexOf(Path.GetExtension(fnl[f + 1])):-1; if (x == -1)
         {
             try{ vc.ConvertMedia(fdI + fn, null, dn, fmt, vcs); }
             catch (FFMpegException e) { System.Diagnostics.Debug.WriteLine("Err{0}: {1}", e.ErrorCode, e.Message); }
         }
         else
         {
             f++; fz += new FileInfo(fnl[f]).Length;
             try{ vc.ConvertMedia(new FFMpegInput[] { new FFMpegInput(fdI + fn), new FFMpegInput(fnl[f]) }, dn, fmt, vcs); }
             catch (FFMpegException e) { System.Diagnostics.Debug.WriteLine("Err{0}: {1}", e.ErrorCode, e.Message); }
         }
         long f2 = new FileInfo(dn).Length;
         System.Diagnostics.Debug.WriteLine("Delete {0}? {1}", fn, (f2 * 100) / fz);
         //if((f2*100)/fz>80) File.Delete(fdI+fn);
     }
 }
コード例 #2
0
ファイル: Converter.cs プロジェクト: yellow444/VideoConverter
        // Converts a single media file.
        //
        // \param path file - The file to be converted.
        // \param int toFormatcomboBoxIndex - The index of the format to be converted to.
        // \param int fromFormatcomboBoxIndex - The index of the format to be converted from.
        // \return bool - True if media file is converted correctly otherwise false.
        //
        public bool ConvertFile(string file, int toFormatcomboBoxIndex, int fromFormatcomboBoxIndex, bool deleteAll, ConvertSettings VideoSettings)
        {
            // The array of supported file formats
            string[] formats =
            {
                Format.mp4, Format.matroska, Format.avi,
                Format.mov, Format.mpeg,     Format.ogg,
                Format.flv, Format.webm
            };

            // If the file is a media file and it is not already the correct format.
            if (((fromFormatcomboBoxIndex == 0 && IsMediaFile(file)) || (fromFormatcomboBoxIndex != 0 &&
                                                                         Path.GetExtension(file) == mediaExtensions[fromFormatcomboBoxIndex - 1])) && Path.GetExtension(file) != mediaExtensions[toFormatcomboBoxIndex])
            {
                // Determine which file is currently being processed.
                VideoConverterForm.currentFile = "Currently Processing: " + Path.GetFileName(file);

                // Create the new file name.
                string newFile   = file.Substring(0, file.Length - Path.GetExtension(file).Length) + mediaExtensions[toFormatcomboBoxIndex];
                string format    = formats[toFormatcomboBoxIndex];
                string oldFormat = Path.GetExtension(file).Substring(1);
                if (oldFormat == "mkv")
                {
                    oldFormat = Format.matroska;
                }

                try
                {
                    // Converts the file to the selected format.
                    ffMpeg.ConvertMedia(file, oldFormat, newFile, format, VideoSettings);

                    // Deletes the files if the checkbox is checked.
                    if (deleteAll)
                    {
                        File.Delete(file);
                    }

                    return(true);
                }
                catch
                {
                    MessageBox.Show("Error Detected in File " + Path.GetFileName(file) + " Skipping");
                    if (File.Exists(newFile))
                    {
                        File.Delete(newFile);
                    }
                    return(false);
                }
            }
            return(false);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: Gavamot/VideoConvector
        private static void Convert(string path, string[] ext, bool delNotConv)
        {
            var files = new List <string>();

            GetFiles(path, ref files);
            if (files.Count > 0)
            {
                Console.WriteLine("Конвертиция файлов в mp4 началась");
                var err = new List <string>();
                for (int i = 0; i < files.Count; i++)
                {
                    string tmpFile = string.Empty;
                    try
                    {
                        var finfo = new FileInfo(files[i]);
                        if (ext.Contains(finfo.Extension))
                        {
                            Console.WriteLine($"{i}/{files.Count} - {files[i]} ");
                            tmpFile = files[i] + Guid.NewGuid();
                            FFMpegConverter ffMpeg = new FFMpegConverter();
                            ffMpeg.ConvertMedia(files[i], tmpFile, Format.mp4);
                            File.Delete(files[i]);
                            File.Move(tmpFile, files[i]);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(e.Message);
                        err.Add($"Ошибка конвертации файла {i}/{files.Count} - {files[i]} ({e.Message})");
                        if (delNotConv)
                        {
                            Console.WriteLine($"Файл будет удален {i}/{files.Count} - {files[i]}");
                            try
                            {
                                File.Delete(files[i]);
                                if (!string.IsNullOrEmpty(tmpFile))
                                {
                                    try { File.Delete(tmpFile); }catch (Exception ex) { }
                                }
                            }
                            catch (Exception ex)
                            {
                                var msg = $"Не удалось удалить файл {i}/{files.Count} - {files[i]} ({ex.Message})";
                                Console.WriteLine(msg);
                                err.Add(msg);
                            }
                        }
                        Console.ResetColor();
                    }
                }

                Console.WriteLine("Конвертиция файлов завершена");
                Console.ForegroundColor = ConsoleColor.Red;
                foreach (var e in err)
                {
                    Console.WriteLine(e);
                }
            }
            else
            {
                Console.WriteLine("Файлов для конвертации не обнаружено.");
            }
            Console.WriteLine("Нажмите любую клавишу ....");
            Console.ReadKey();
        }
コード例 #4
0
ファイル: Form1.cs プロジェクト: bilalrcc/VideoConverter
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //var result = openFileDialog1.ShowDialog();
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                FileInfo fi = new FileInfo(ofd.FileName);
                fExt  = fi.Extension;
                fName = fi.FullName.Substring(0, fi.FullName.Length - fExt.Length);
                //if (!fExt.Equals(".wmv"))
                //{
                //    MessageBox.Show("Only WMV file type is supported");
                //}
                //else
                //{ }
                try
                {
                    var x       = false;
                    var message = "";
                    var input   = ofd.FileName;//Path.Combine(@"D:\Dev\Company\Exalted Solutions 2016 July\Projects\CaliberMatrix-Development\CaliberMatrix\SeleniumTestCaseRecording\SeleniumTest-3034ef9ce96dfb5b02fb76a04851ae61", "Video-2017-04-07_12-02-10-AM.wmv");
                    if (!File.Exists(input))
                    {
                        MessageBox.Show(@"File does not exist", @"Error", MessageBoxButtons.OK);
                    }
                    else
                    {
                        var fileName = Path.GetFileNameWithoutExtension(input);

                        var outputFileNameOgg  = fileName + ".ogg";
                        var outputFileNameMp4  = fileName + ".mp4";
                        var outputFileNameWebm = fileName + ".webm";

                        var bytes = File.ReadAllBytes(input);
                        var data  = new MemoryStream(bytes);
                        var t     = Task.Factory.StartNew(() =>
                        {
                            try
                            {
                                //backgroundWorker1.RunWorkerAsync();
                                var ffMpeg = new FFMpegConverter();
                                //backgroundWorker1.RunWorkerAsync();
                                ffMpeg.ConvertProgress += UpdateProgress;
                                //ffMpeg.ConvertMedia(input, outputFileNameWebm, Format.webm);
                                //ffMpeg.ConvertMedia(input, outputFileNameOgg, Format.ogg);
                                //ffMpeg.ConvertMedia(input, outputFileNameMp4, Format.mp4);
                                ffMpeg.ConvertMedia(input, null, outputFileNameMp4, null, new ConvertSettings()
                                {
                                    CustomOutputArgs = "-profile:v baseline -level 3.0 -pix_fmt yuv420p -threads 2 -movflags +faststart"
                                });

                                x       = true;
                                p       = 100;
                                message = "Video converted successfully";
                                return(x);
                            }
                            catch (Exception ex)
                            {
                                p       = 100;
                                x       = false;
                                message = ex.GetBaseException().Message;
                                return(x);
                            }
                        });
                        t.Wait();
                        if (t.IsCompleted)
                        {
                            if (x)
                            {
                                MessageBox.Show(message, @"Success", MessageBoxButtons.OK);
                            }
                            else
                            {
                                MessageBox.Show(message, @"Error", MessageBoxButtons.OK);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.GetBaseException().Message, @"Error", MessageBoxButtons.OK);
                }
            }
        }