Click here to Skip to main content
12,509,509 members (61,658 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
Hello everybody.
I've tried next lines:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "ffmpeg";
p.StartInfo.Arguments = " -h ";
p = Process.Start(p.StartInfo);
this.textBox1.Text = p.StandardOutput.ReadToEnd();

In this case everything looks fine and TextBox have got a output of ffmpeg.exe.
But in case of real job:
p.StartInfo.Arguments = " -i input.avi -y output.ts ";
I can see just empty TextBox although convertion has been completed and output.ts has been created.

Thanks in advance.
Posted 11-Nov-12 3:40am
Updated 11-Nov-12 6:47am
Pablo Aliskevicius 11-Nov-12 9:58am
What is the output if you run the command on a console window?
What happens if you call ReadToEnd() again after WaitForExit()?
werfog 11-Nov-12 10:52am
1. I don't know how to add a picture to my reply. The text of console is

C:\Users\ааа\Documents\Visual Studio 2010\Projects\C#\VisualFFmpeg_1\bin\Debug>f
fmpeg.exe -i E:\input.avi -y E:\output.ts
ffmpeg version N-46055-g5f0105b Copyright (c) 2000-2012 the FFmpeg developers
built on Oct 26 2012 17:51:52 with gcc 4.7.2 (GCC)
configuration: --enable-gpl --enable-version3 --disable-pthreads --enable-runt
ime-cpudetect --enable-avisynth --enable-bzlib --enable-frei0r --enable-libass -
-enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libfreetype --enab
le-libgsm --enable-libmp3lame --enable-libnut --enable-libopenjpeg --enable-libo
pus --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheo
ra --enable-libutvideo --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-li
bvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --ena
libavutil 52. 0.100 / 52. 0.100
libavcodec 54. 69.100 / 54. 69.100
libavformat 54. 35.100 / 54. 35.100
libavdevice 54. 3.100 / 54. 3.100
libavfilter 3. 20.106 / 3. 20.106
libswscale 2. 1.101 / 2. 1.101
libswresample 0. 16.100 / 0. 16.100
libpostproc 52. 1.100 / 52. 1.100
Input #0, avi, from 'E:\input.avi':
encoder : Lavf51.12.1
Duration: 00:01:00.13, start: 0.000000, bitrate: 4218 kb/s
Stream #0:0: Video: mpeg4 (Simple Profile) (xvid / 0x64697678), yuv420p, 640
x360 [SAR 1:1 DAR 16:9], 29.97 tbr, 29.97 tbn, 29.97 tbc
Stream #0:1: Audio: mp3 (U[0][0][0] / 0x0055), 44100 Hz, stereo, s16, 128 kb
Output #0, mpegts, to 'E:\output.ts':
encoder : Lavf54.35.100
Stream #0:0: Video: mpeg2video, yuv420p, 640x360 [SAR 1:1 DAR 16:9], q=2-31,
200 kb/s, 90k tbn, 29.97 tbc
Stream #0:1: Audio: mp2, 44100 Hz, stereo, s16, 128 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (mpeg4 -> mpeg2video)
Stream #0:1 -> #0:1 (mp3 -> mp2)
Press [q] to stop, [?] for help
frame= 227 fps=0.0 q=31.0 size= 999kB time=00:00:07.72 bitrate=1059.6kbits/
frame= 476 fps=462 q=31.0 size= 1849kB time=00:00:16.00 bitrate= 946.6kbits/
frame= 742 fps=480 q=31.0 size= 2748kB time=00:00:24.85 bitrate= 905.5kbits/
frame= 1005 fps=488 q=24.8 size= 3648kB time=00:00:33.66 bitrate= 887.8kbits/
frame= 1272 fps=494 q=31.0 size= 4519kB time=00:00:42.56 bitrate= 869.7kbits/
frame= 1526 fps=494 q=31.0 size= 5402kB time=00:00:51.05 bitrate= 866.7kbits/
frame= 1790 fps=497 q=31.0 size= 6295kB time=00:00:59.86 bitrate= 861.5kbits/
frame= 1800 fps=497 q=31.0 Lsize= 6339kB time=00:01:00.12 bitrate= 863.7kbits
video:4708kB audio:940kB subtitle:0 global headers:0kB muxing overhead 12.233324

C:\Users\ааа\Documents\Visual Studio 2010\Projects\C#\VisualFFmpeg_1\bin\Debug>p
Для продолжения нажмите любую клавишу . . .

2. Nothing is shown again in the TextBox.
Thanks for your attention.
Pablo Aliskevicius 12-Nov-12 4:21am
My guess is that there is some kind of race condition: ReadToEnd() may get called before ffmpeg writes to the standard output. But, it's just a guess...
I've upvoted the question in the hope of luring someone who knows better.
Best wishes,
werfog 12-Nov-12 6:43am
Thank you very much.
I wrote that in the hope that someone who encoutered such a problem and solved that could help.
I'm trying to sort out that trouble. And I suspect that answer lies somewhere in Apartment Threading. But it's hard to understand for me.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The best and the shortest solution was found in the article
' Video Transcoding "YouTube-Style" with ffmpeg ' posted by Jonathan Lathigee.
Video Transcoding "YouTube-Style" with ffmpeg
He writes:
"Another problem came in the capturing of output from FFMPEG to get all of the file's metadata. I created a new process and streamReader, and captured the StandardOutput from the process. Nothing. Turns out that there is no specific command in FFMPEG to read metadata from a video file, instead this info comes out as a StandardError stream when FFMPEG is called with no output file specified. Once I switched from capturing the StandardOutput to StandardError, I had no problems."

And that was key factor for me. As I tried to catch StandardOutput of ffmpeg instead of catching StandardError. So I got all necessary lines into my TextBox. Finally I used asynchronous reading that was more suitable for my purpose. Here is the code.

                Process p = this.process1;
                String input = " -i " + this.listBox2.Text; //  /c ffmpeg.exe // for launching ffmpeg via cmd.exe
                String size = " -s " + this.comboBox1.Text;
                String codec = " -vcodec " + this.comboBox2.Text;
                String encoder = " -vtag " + this.comboBox3.Text;
                String quality = " -qscale " + this.comboBox4.Text;
                String videobr = " -b:v " + this.comboBox4.Text + "k";
                String audiobr = " -ab " + this.comboBox5.Text + "k";
                String output = " -y " + this.textBox2.Text;
                String report = " 2> \"" + this.textBox2.Text + ".log\""; // for launching ffmpeg via cmd.exe and putting output into file
                String s = input + size + codec + encoder + videobr + audiobr + output;// +report;
                String sa = " -h ";// help from ffmpeg
                p.StartInfo.Arguments = s;
                MessageBox.Show("Waiting for the process to exit....");
                if (p.HasExited) { p.CancelErrorRead(); p.CancelOutputRead(); p.Close(); MessageBox.Show("Closed"); }
            catch (Exception ex)
Following ErrorDataReceived handler.
private void process1_ErrorDataReceived(object sender, DataReceivedEventArgs e)
            this.textBox1.Text += e.Data;
            //MessageBox.Show("4.Error\n" + e.Data);

You can find some extra lines: MessageBoxes etc. they are just for debugging.
So Great Thanks to Jonathan Lathigee and 5 stars for him.
I'm glad if my experience is usefull for you.
Best wishes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160927.1 | Last Updated 13 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100