Exemplo n.º 1
0
        private void StartOneThread()
        {
            int isHandling = -1;

            while (true)
            {
                lock (handledlock)
                {
                    hasHandle++;
                    isHandling = hasHandle;
                }
                if (isHandling >= listView1.Items.Count)
                {
                    return;
                }
                ListViewItem item = null;

                this.Invoke((Action) delegate()
                {
                    item = listView1.Items[isHandling];
                    listView1.Items[isHandling].SubItems["States"].Text = "转码中";
                });
                AudioInfo info = item.Tag as AudioInfo;

                string output = string.Empty;
                string copyto = string.Empty;
                if (cbKeepFileTree.Checked)
                {
                    string ralative = FileUtility.MakeRelativePath(info.DirPath + "/", Path.GetDirectoryName(info.FullName));
                    string outpath  = Path.Combine(textBox1.Text, ralative);
                    output = Path.Combine(outpath, Path.GetFileNameWithoutExtension(info.FullName) + ".m4a");
                    string copytopath = Path.Combine(textBox2.Text, ralative);
                    copyto = Path.Combine(copytopath, Path.GetFileNameWithoutExtension(info.FullName) + ".m4a");
                    if (!Directory.Exists(outpath))
                    {
                        Directory.CreateDirectory(outpath);
                    }
                    if (!Directory.Exists(copyto))
                    {
                        Directory.CreateDirectory(copyto);
                    }
                }
                else
                {
                    output = Path.Combine(textBox1.Text, Path.GetFileNameWithoutExtension(info.FullName) + ".m4a");
                    copyto = Path.Combine(textBox2.Text, Path.GetFileNameWithoutExtension(info.FullName) + ".m4a");
                }

                string           bat         = getbat(info.FullName, output);
                ProcessStartInfo processinfo = new ProcessStartInfo();
                processinfo.FileName               = Environment.GetEnvironmentVariable("ComSpec");
                processinfo.Arguments              = "/c " + bat;
                processinfo.UseShellExecute        = false; //输出信息重定向
                processinfo.CreateNoWindow         = true;
                processinfo.RedirectStandardInput  = true;
                processinfo.RedirectStandardOutput = true;
                processinfo.RedirectStandardError  = false;
                processinfo.WindowStyle            = ProcessWindowStyle.Hidden;
                Process ffmpeg = new Process();
                ffmpeg.StartInfo = processinfo;
                ffmpeg.Start();

                var result = ffmpeg.StandardOutput.ReadToEnd();
                ffmpeg.WaitForExit();
                //ffmpeg.Kill();//等待进程结束
                ffmpeg.Dispose();

                if (cbCopyID3.Checked == true)
                {
                    try
                    {
                        using (var mp3 = new Mp3File(info.FullName))
                        {
                            if (mp3.HasTagOfFamily(Id3TagFamily.FileStartTag))
                            {
                            }
                            else
                            {
                                using (var outputmp3 = new Mp3File(output, Mp3Permissions.ReadWrite))
                                {
                                    Id3v1Tag id3tag = Id3Tag.Create <Id3v1Tag>();
                                    TagConsole.ReadTagToID3(id3tag, info.FullName);
                                    outputmp3.WriteTag(id3tag, id3tag.MajorVersion, id3tag.MinorVersion, WriteConflictAction.Replace);
                                }
                            }
                        }
                    }
                    catch { }
                }
                if (cbCompleteDo.Checked)
                {
                    try
                    {
                        if (cblCompeteAction.SelectedIndex == 0)
                        {
                            this.Invoke((Action) delegate()
                            {
                                listView1.Items[isHandling].SubItems["States"].Text = "拷贝中";
                            });

                            File.Copy(output, copyto, true);
                        }
                        else if (cblCompeteAction.SelectedIndex == 1)
                        {
                            this.Invoke((Action) delegate()
                            {
                                listView1.Items[isHandling].SubItems["States"].Text = "剪切中";
                            });
                            if (File.Exists(copyto))
                            {
                                File.Delete(copyto);
                            }
                            File.Move(output, copyto);
                        }
                    }
                    catch { }
                }

                this.Invoke((Action) delegate()
                {
                    listView1.Items[isHandling].SubItems["States"].Text = "完成";
                });
            }
        }
Exemplo n.º 2
0
        private void StartOneThread()
        {
            int isHandling = -1;

            while (true)
            {
                lock (handledlock)
                {
                    hasHandle++;
                    isHandling = hasHandle;
                }
                if (isHandling >= listView1.Items.Count)
                {
                    return;
                }
                ListViewItem item = null;

                this.Invoke((Action) delegate()
                {
                    item = listView1.Items[isHandling];
                    listView1.Items[isHandling].SubItems["States"].Text = "转码中";
                });
                AudioInfo info = item.Tag as AudioInfo;

                string extension = ".m4a";

                if (cbAudioEncoder.Text == "Opus")
                {
                    extension = ".opus";
                }
                else if (cbAudioEncoder.Text == "FLAC")
                {
                    extension = ".flac";
                }

                string output = string.Empty;
                string copyto = string.Empty;
                if (cbKeepFileTree.Checked)
                {
                    string ralative = FileUtility.MakeRelativePath(info.DirPath + "/", Path.GetDirectoryName(info.FullName));
                    string outpath  = Path.Combine(textBox1.Text, ralative);
                    output = Path.Combine(outpath, Path.GetFileNameWithoutExtension(info.FullName) + extension);
                    string copytopath = Path.Combine(textBox2.Text, ralative);
                    copyto = Path.Combine(copytopath, Path.GetFileNameWithoutExtension(info.FullName) + extension);
                    if (!Directory.Exists(outpath))
                    {
                        Directory.CreateDirectory(outpath);
                    }
                    if (!Directory.Exists(copyto))
                    {
                        Directory.CreateDirectory(copyto);
                    }
                }
                else
                {
                    output = Path.Combine(textBox1.Text, Path.GetFileNameWithoutExtension(info.FullName) + extension);
                    copyto = Path.Combine(textBox2.Text, Path.GetFileNameWithoutExtension(info.FullName) + extension);
                }

                AudioConfig ac = new AudioConfig();
                ac.Encoder = info.Encoder;
                ac.Input   = info.FullName;
                ac.Quality = float.Parse(txtQuality.Text);
                int.TryParse(txtCh.Text, out ac.Channel);
                if (string.IsNullOrEmpty(output))
                {
                    output = Path.GetFileNameWithoutExtension(ac.Input) + extension;
                }
                ac.Output = output;

                if (info.Encoder == AudioEncoder.aac)
                {
                    CommandHelper.RunFFmpegToAAC(ac);
                }
                else if (info.Encoder == AudioEncoder.opus)
                {
                    CommandHelper.RunFFmpegToOpus(ac);
                }
                else if (info.Encoder == AudioEncoder.flac)
                {
                    CommandHelper.RunFFmpegToFlac(ac);
                }


                if (cbCopyID3.Checked == true && ac.Encoder == AudioEncoder.aac)
                {
                    try
                    {
                        using (var mp3 = new Mp3File(info.FullName))
                        {
                            if (mp3.HasTagOfFamily(Id3TagFamily.FileStartTag))
                            {
                            }
                            else
                            {
                                using (var outputmp3 = new Mp3File(output, Mp3Permissions.ReadWrite))
                                {
                                    Id3v1Tag id3tag = Id3Tag.Create <Id3v1Tag>();
                                    TagConsole.ReadTagToID3(id3tag, info.FullName);
                                    outputmp3.WriteTag(id3tag, id3tag.MajorVersion, id3tag.MinorVersion, WriteConflictAction.Replace);
                                }
                            }
                        }
                    }
                    catch { }
                }
                if (cbCompleteDo.Checked)
                {
                    try
                    {
                        if (cblCompeteAction.SelectedIndex == 0)
                        {
                            this.Invoke((Action) delegate()
                            {
                                listView1.Items[isHandling].SubItems["States"].Text = "拷贝中";
                            });

                            File.Copy(output, copyto, true);
                        }
                        else if (cblCompeteAction.SelectedIndex == 1)
                        {
                            this.Invoke((Action) delegate()
                            {
                                listView1.Items[isHandling].SubItems["States"].Text = "剪切中";
                            });
                            if (File.Exists(copyto))
                            {
                                File.Delete(copyto);
                            }
                            File.Move(output, copyto);
                        }
                    }
                    catch { }
                }

                this.Invoke((Action) delegate()
                {
                    listView1.Items[isHandling].SubItems["States"].Text = "完成";
                });
            }
        }