C# WinApp ffmpeg ไม่ทราบว่าข้อมูลแบบนี้จะดึงมาใช้ใน progress bar ยังไง ครับ
Code
H:\New folder>"H:\ffmpeg\ffmpeg.exe" -i "H:\New folder\Dragon_Ball_132.mkv" -s 720x480 "H:\New folder\Dragon_Ball_132.mp4"
ffmpeg version 3.3.2 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 7.1.0 (GCC)
configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-cuda --enable-cuvid --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-zlib
libavutil 55. 58.100 / 55. 58.100
libavcodec 57. 89.100 / 57. 89.100
libavformat 57. 71.100 / 57. 71.100
libavdevice 57. 6.100 / 57. 6.100
libavfilter 6. 82.100 / 6. 82.100
libswscale 4. 6.100 / 4. 6.100
libswresample 2. 7.100 / 2. 7.100
libpostproc 54. 5.100 / 54. 5.100
Input #0, matroska,webm, from 'H:\New folder\Dragon_Ball_132.mkv':
Metadata:
encoder : libebml v1.0.0 + libmatroska v1.0.0
creation_time : 2011-02-11T02:40:11.000000Z
Duration: 00:24:38.98, start: 0.000000, bitrate: 1026 kb/s
Chapter #0:0: start 0.054000, end 1478.978000
Metadata:
title : 00:00:00.054
Stream #0:0(eng): Video: h264 (High), yuv420p(progressive), 720x480 [SAR 4320:4739 DAR 6480:4739], SAR 167:183 DAR 167:122, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
Stream #0:1(tha): Audio: aac (LC), 48000 Hz, stereo, fltp (default)
Stream #0:2(jpn): Audio: aac (LC), 48000 Hz, stereo, fltp (forced)
Stream #0:3(tha): Subtitle: dvd_subtitle, 720x480 (default)
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0000000002680560] using SAR=167/183
[libx264 @ 0000000002680560] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0000000002680560] profile High, level 3.0
[libx264 @ 0000000002680560] 264 - core 152 r2851 ba24899 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=23 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'H:\New folder\Dragon_Ball_132.mp4':
Metadata:
encoder : Lavf57.71.100
Chapter #0:0: start 0.054000, end 1478.978000
Metadata:
title : 00:00:00.054
Stream #0:0(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 720x480 [SAR 167:183 DAR 167:122], q=-1--1, 23.98 fps, 24k tbn, 23.98 tbc (default)
Metadata:
encoder : Lavc57.89.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
Stream #0:1(tha): Audio: aac (LC) ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
encoder : Lavc57.89.100 aac
frame=18175 fps=139 q=28.0 size= 70082kB time=00:12:38.22 bitrate= 757.2kbits/s speed=5.79x
ผมอยากทำเป็น progress bar เพื่อดูความคืบหน้าของงาน ไม่ทราบดึงข้อมูลตรงไหนมาใช้ครับTag : .NET, Win (Windows App), C#
Date :
2020-12-27 18:21:26
By :
lamaka.tor
View :
998
Reply :
10
ผมลองดึง data มาจาก OutputDataReceived
Code (C#)
private void ConvertffmpegItem(string f)
{
string file = f;
string _file = Path.GetDirectoryName(file) + "\\" + Path.GetFileNameWithoutExtension(file) + ".mp4";
txtStatus.Text = "ConvertffmpegItem:" + f;
Process process = new Process
{
StartInfo =
{
FileName = ffmpeg_file,
Arguments = string.Format(ffmpeg_query,file, _file,display),
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Normal
},
EnableRaisingEvents = true
};
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
{
string consoleLine = e.Data;
if (!string.IsNullOrWhiteSpace(consoleLine))
ParseffmpegProgress(consoleLine);
};
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
{
string consoleLine = e.Data;
if (!string.IsNullOrWhiteSpace(consoleLine))
ParseffmpegProgress(consoleLine);
};
process.Exited += new EventHandler((object _s, EventArgs _e) =>
{
process.Dispose();
backgroundWorker1.Invoke(new Action(() => backgroundWorker1.Value++));
index++;
if (index >= dataGridView1.Rows.Count)
{ MessageBox.Show("ConvertMKV All Complete"); return; }
ConvertMKVItem();
});
process.Start();
}
private void ParseffmpegProgress(string consoleLine)
{
// Duration: 00:30:58.39, start: 0.023021, bitrate: 106 kb/s
// size= 105kB time=00:00:06.69 bitrate= 128.7kbits/s speed=13.4x
/*
Past duration 0.995995 too large
frame= 624 fps= 60 q=28.0 size= 4241kB time=00:00:26.11 bitrate=1330.7kbits/s dup=1 drop=0 speed=2.51x
*/
Regex extractDuration = new Regex(@"Duration:\s*([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{2}),\sstart:");
double Duration=0d;
Match match = extractDuration.Match(consoleLine);
txtStatus.Text = "Duration:" + match.Groups[1].Value;
if (match.Length > 0 && match.Groups.Count >= 1)
{
Duration = TimeSpan.Parse(match.Groups[1].Value).TotalSeconds;
if (Duration == 0d)
{
return;
}
Regex extractProgressDuration = new Regex(@"time=([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{2})\s*bitrate=");
match = extractProgressDuration.Match(consoleLine);
if (match.Length > 0 && match.Groups.Count >= 1)
{
dataGridView1.Invoke(new Action(() => dataGridView1[1, index].Value = (int)(70 + TimeSpan.Parse(match.Groups[1].Value).TotalSeconds / Duration * 60)));
}
}
}
แต่กลับไม่มีข้อมูลกลับมาหรือ รันไปถึง ParseffmpegProgress ตามโค้ดข้างต้น ครับ
Date :
2020-12-27 21:45:31
By :
lamaka.tor
ตอนนี้ทำไปถึงไหนแล้ว ประสบผลหรือยัง
Date :
2020-12-28 18:26:19
By :
genius
คุณก็อปจาก SO มามันทำไม่ได้ก็ไปวิธีใหม่ตามที่เขาแนะนำ อย่าดันทุรัง
วิธีดึงจาก CMD/Console มา .NET (คัดลอกจากลิงค์ NO.1 มาให้)
Code (C#)
using System;
using System.Diagnostics;
using System.IO;
class Test {
static void Main(string[] args) {
Process proc = new Process();
proc.StartInfo.FileName = "ffmpeg";
proc.StartInfo.Arguments = "-i " + args[0] + " " + args[1];
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
if (!proc.Start()) {
Console.WriteLine("Error starting");
return;
}
StreamReader reader = proc.StandardError;
string line;
while ((line = reader.ReadLine()) != null) {
Console.WriteLine(line);
}
proc.Close();
}
}
ที่ทำไปโล๊ะทิ้งทั้งหมด ให้ดึง frame มาให้ได้ก่อน
Date :
2020-12-28 22:14:58
By :
lakornworld
แจ้งความคืบหน้าครับ
Code (C#)
private void Form2_Load(object sender, EventArgs e)
{
MediaFile mediaFile = new MediaFile(@"j:\test.mkv");
string strRegex = @"Output #0.*?\n\s{0,}.*?\n\s{0,}.*?\n\s{0,}.*?\n\s{0,}.*?\n\s{0,}.*?\n\s{0,}.*?\n\s{0,}NUMBER_OF_FRAMES.*?:\s{1,}(\d+)\n";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strRegex_ = @"frame=\s{1,}(\d+)";
Regex myRegex_ = new Regex(strRegex_, RegexOptions.None);
long frameAll = 0;
string ffmpeg_query = " -i \"{0}\" -s {2} \"{1}\"";
string mkvmerge_file = Application.StartupPath + @"\MKVToolNix\mkvmerge.exe";
string ffmpeg_file = Application.StartupPath + @"\ffmpeg\ffmpeg.exe";
string str = "";
Task.Run(() =>
{
Process proc = new Process();
proc.StartInfo.FileName = ffmpeg_file;
proc.StartInfo.Arguments = string.Format(ffmpeg_query, @"j:\test.mkv", @"j:\test.mp4", "1280x720");
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
if (!proc.Start())
{
return;
}
StreamReader reader = proc.StandardError;
// richTextBox1.Invoke(new Action(() => richTextBox1.Text += "\n" + reader.ReadLine()));
string line = "";
while ((line = reader.ReadLine()) != null)
{
str +="\n"+ line;
richTextBox1.Invoke(new Action(() =>
{
richTextBox1.Text = line;
if (richTextBox1.Text.Contains("frame= "))
{
if (frameAll <= 0)
{
Match match = myRegex.Match(str);
if (match.Length > 0 && match.Groups.Count >= 1)
{
frameAll = long.Parse(match.Groups[1].Value);
}
}
Match match_ = myRegex_.Match(richTextBox1.Text);
if (match_.Length > 0 && match_.Groups.Count >= 1)
{
long frame = long.Parse(match_.Groups[1].Value);
if (frame >= 0)
{
this.Invoke(new Action(() => this.Text = "Frame:" + frame + "/" + frameAll + " " + (Convert.ToDouble(frame )/ Convert.ToDouble( frameAll)*100d).ToString("0.000")+ " %"));
}
}
}
}));
// System.Threading.Thread.Sleep(1000);
}
proc.Close();
});
}
ยังไม่ลองใช้งานจริงดูนะครับ
ถ้าลงโค้ดจริงแล้ว เดี๋ยวจะ อัพโค้ดใหม่อีกทีนะครับ
ขอขอบคุณ ท่าน ละครชีวิต และ No. 1 ด้วยนะครับ
Date :
2020-12-29 12:07:55
By :
lamaka.tor
Load balance : Server 05