/// <summary> /// だいたい0.3秒おきの動画サムネイルを取得する /// </summary> /// <param name="videoPath">ビデオファイルパス</param> /// <returns></returns> public static IEnumerable <Bitmap> GetThumbnails(string videoPath) { // メタ情報で再生時間取得 var ffprob = new NReco.VideoInfo.FFProbe(); var info = ffprob.GetMediaInfo(videoPath); var duration = info.Duration; var videoConv = new NReco.VideoConverter.FFMpegConverter(); // 最短0.3秒おき、最大100分割 var skipSec = duration.TotalSeconds < 30 ? 0.3f : (float)(duration.TotalSeconds / 100); var frameSec = 0f; while ((float)duration.TotalSeconds > frameSec) { // Bitmapリソースは受信側で開放する。 var jpegStream = new MemoryStream(); videoConv.GetVideoThumbnail(videoPath, jpegStream, frameSec); yield return((Bitmap)Image.FromStream(jpegStream)); frameSec += skipSec; } }
private void BtnConvert_Click(object sender, EventArgs e) { this.btnBrowse.Enabled = false; this.btnConvert.Enabled = false; this.btnDestination.Enabled = false; this.listAudio.Items.Clear(); int i = 0; this.lblCurrentFile.Visible = true; this.pbProgress.Visible = true; this.listVideo.Visible = true; this.listAudio.Visible = true; this.pbTotalProgress.Visible = true; this.pbProgress.Maximum = 100; this.pbTotalProgress.Maximum = 100; this.totalItem = this.listVideo.Items.Count; this.pbTotalProgress.Value = 0; this.processedItem = 0; foreach (string file in this.input) { try { this.pbProgress.Value = 0; this.processedItem = i + 1; //var item = this.listVideo.Items[i]; //string outputFile = item.ToString(); var outputFile = Path.GetFileNameWithoutExtension(file); //outputFile.Substring(0, outputFile.LastIndexOf('.')); this.lblCurrentFile.Text = "Converting: " + file; var ConvertToAudio = new NReco.VideoConverter.FFMpegConverter(); var ffprobe = new NReco.VideoInfo.FFProbe(); MediaInfo videoInfo = ffprobe.GetMediaInfo(file); this.totalSeconds = videoInfo.Duration.Milliseconds; ConvertToAudio.ConvertProgress += new EventHandler <ConvertProgressEventArgs>(this.UpdateProgress); ConvertToAudio.ConvertMedia(file, this.txtAudio.Text + "\\" + outputFile + ".mp3", "mp3"); this.listAudio.Items.Add(this.txtAudio.Text + "\\" + outputFile + ".mp3"); i++; } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); i++; } } this.lblCurrentFile.Text = "Completed"; this.btnBrowse.Enabled = true; this.btnConvert.Enabled = true; this.btnDestination.Enabled = true; }
public void run(bool to1dir, bool allFiles, List <OutputFileObj> videos, List <OutputFileObj> others, MainWindow window, CodecOption codec, AudioOption audioOp, ResolutionOption res, QualityOption quality, VideoTypeOption typeOp) { Console.WriteLine( "Running Configurartion:" + "\n Codec: " + Enum.GetName(typeof(CodecOption), codec) + "\n Audio Codec: " + Enum.GetName(typeof(AudioOption), audioOp) + "\n Video Resolution: " + Enum.GetName(typeof(ResolutionOption), res) + "\n Video Type: " + Enum.GetName(typeof(VideoTypeOption), typeOp)); int totalVideos = videos.Count * 100; window.Dispatcher.Invoke(() => window.allVideoFilesProgressBar.Maximum = 100); Converter = new FFMpegConverter(); int index = 0; MainWindow _window = window; Thread _t = Thread.CurrentThread; Converter.ConvertProgress += (sender, e) => { try { _window.Dispatcher.Invoke(() => { Console.WriteLine("Processed: " + e.Processed); _window.currFileProgressBar.Value = (float)e.Processed.TotalSeconds / (float)e.TotalDuration.TotalSeconds * 100; Console.WriteLine((float)e.Processed.TotalSeconds / (float)e.TotalDuration.TotalSeconds * 100); Console.WriteLine(_window.currFileProgressBar.Value); _window.allVideoFilesProgressBar.Value = 100 / (float)videos.Count * index + (float)e.Processed.TotalSeconds / (float)e.TotalDuration.TotalSeconds * 100 / (float)videos.Count; }); } catch (TaskCanceledException tce) { _t.Abort(); } }; foreach (OutputFileObj video in videos) { var videoMedia = new NReco.VideoInfo.FFProbe().GetMediaInfo(video.Path); window.Dispatcher.Invoke(() => { window.currFileProgressBar.Maximum = 100; }); if (typeOp != VideoTypeOption.copy) { video.OutPath = System.IO.Path.ChangeExtension(video.OutPath, "." + Enum.GetName(typeof(VideoTypeOption), typeOp)); } if (audioOp == AudioOption.flac && System.IO.Path.GetExtension(video.Path).ToLower() == "." + Enum.GetName(typeof(VideoTypeOption), VideoTypeOption.mp4).ToLower()) { audioOp = AudioOption.copy; } string scaleString = res == ResolutionOption.copy ? "" : res == ResolutionOption._1080 ? "-vf scale=-2:1080" : res == ResolutionOption._720 ? "-vf scale=-2:720" : res == ResolutionOption._480 ? "-vf scale=-2:480" : res == ResolutionOption._240 ? "-vf scale=-2:240" : ""; Func <string> getCopyCodec = () => { string videoCodec = videoMedia.Streams.ToList().Find( (item) => item.CodecType.ToLower() == "video").CodecName; if (videoCodec.Contains("h264")) { return("libx264"); } if (videoCodec.Contains("hevc")) { return("libx265"); } return("libx264"); }; string codecString = codec == CodecOption.copy ? getCopyCodec() : codec == CodecOption.h264 ? "libx264" : codec == CodecOption.HEVC ? "libx265" : getCopyCodec(); string Command = string.Format( "-c:v {3} {0} -crf {1} -c:a {2} -stats -y", scaleString, (int)quality, Enum.GetName(typeof(AudioOption), (int)audioOp), codecString); Console.WriteLine("Input Path: '" + video.Path + "'"); Console.WriteLine("Output Path: '" + video.OutPath + "'"); System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(video.OutPath)); Console.WriteLine("Created direcotory: '" + System.IO.Path.GetDirectoryName(video.OutPath) + "'"); Console.WriteLine("Running Command: '" + Command + "'"); Converter.ConvertMedia(video.Path, null, video.OutPath, null, new ConvertSettings() { CustomOutputArgs = Command }); Console.WriteLine("Video converted"); index++; } }