c# - How can i compress avi file using ffmpeg? -


i have class in link im using in project:

http://jasonjano.wordpress.com/2010/02/09/a-simple-c-wrapper-for-ffmpeg/

now in form1 im using this: in timer1 tick event create avi file , it's working without problems:

private void timer1_tick(object sender, eventargs e)         {             if (playimages == true)             {                 timer1.enabled = false;                 play();                 timer1.enabled = true;             }             else             {                 af.createavi(this.sc);              }         } 

then compression added new function:

public string converth264(string inputpath, string outpath)         {              string params1 = string.format("-i {0}  -vcodec mpeg4 -acodec aac -strict -2 -ar 22050 -b:a 128k {1}", inputpath, outpath);             string output = _converter.runprocess(params1);             return output;         } 

then in button3 stop timer , avi file on hard disk did:

converth264(@"d:\testdata\new.avi", @"d:\testdata\new.flv"); 

the result on hard disk 2 files:

new.avi , new.flv

the avi file 324mb , flv file 960kb can play avi file can't play flv file anywhere . flv file wrong.

the flv file should compressed one.

what problem ? maybe ffmpeg parameters ?

string params1 = string.format("-i {0}  -vcodec mpeg4 -acodec aac -strict -2 -ar 22050 -b:a 128k {1}", inputpath, outpath); 

if needed: class how im using in link above:

using system; using system.collections.generic; using system.linq; using system.web; using system.io; using system.diagnostics; using system.configuration; using system.text.regularexpressions;  namespace screenvideorecorder {     public class converter     {         #region properties         private string _ffexe;         public string ffexe         {                         {                 return _ffexe;             }             set             {                 _ffexe = value;             }         }          private string _workingpath;         public string workingpath         {                         {                 return _workingpath;             }             set             {                 _workingpath = value;             }         }          #endregion          #region constructors         public converter()         {             initialize();         }         public converter(string ffmpegexepath)         {             _ffexe = ffmpegexepath;             initialize();         }         #endregion          #region initialization         private void initialize()         {             //first make sure have value ffexe file setting             if (string.isnullorempty(_ffexe))             {                 _ffexe = @"d:\temp\compress\ffmpeg\ffmpeg.exe";                  /* if (o == null)                 {                     throw new exception("could not find location of ffmpeg exe file.  path ffmpeg.exe " +                     "can passed in via constructor of ffmpeg class (this class) or setting in app.config or web.config file.  " +                     "in appsettings section, correct property name is: ffmpeg:exelocation");                 }                 else                 {                     if (string.isnullorempty(o.tostring()))                     {                         throw new exception("no value found in app setting ffmpeg:exelocation");                     }                     _ffexe = o.tostring();                 }*/             }              //now see if ffmpeg.exe exists             string workingpath = getworkingfile();             if (string.isnullorempty(workingpath))             {                 //ffmpeg doesn't exist @ location stated.                 throw new exception("could not find copy of ffmpeg.exe");             }             _ffexe = workingpath;              //now see if have temporary place work             if (string.isnullorempty(_workingpath))             {                 _workingpath = @"d:\temp\compress\ffmpeg\";                /* if (o != null)                 {                     _workingpath = o.tostring();                 }                 else                 {                     _workingpath = string.empty;                 }*/             }         }          private string getworkingfile()         {             //try stated directory             if (file.exists(_ffexe))             {                 return _ffexe;             }              //oops, didn't work, try base directory             if (file.exists(path.getfilename(_ffexe)))             {                 return path.getfilename(_ffexe);             }              //well, unlucky, let's return null             return null;         }         #endregion          #region file without creating file lock         public static system.drawing.image loadimagefromfile(string filename)         {             system.drawing.image theimage = null;             using (filestream filestream = new filestream(filename, filemode.open,             fileaccess.read))             {                 byte[] img;                 img = new byte[filestream.length];                 filestream.read(img, 0, img.length);                 filestream.close();                 theimage = system.drawing.image.fromstream(new memorystream(img));                 img = null;             }             gc.collect();             return theimage;         }          public static memorystream loadmemorystreamfromfile(string filename)         {             memorystream ms = null;             using (filestream filestream = new filestream(filename, filemode.open,             fileaccess.read))             {                 byte[] fil;                 fil = new byte[filestream.length];                 filestream.read(fil, 0, fil.length);                 filestream.close();                 ms = new memorystream(fil);             }             gc.collect();             return ms;         }         #endregion          #region run process         public string runprocess(string parameters)         {             //create process info             processstartinfo oinfo = new processstartinfo(this._ffexe, parameters);             oinfo.useshellexecute = false;             oinfo.createnowindow = true;             oinfo.redirectstandardoutput = true;             oinfo.redirectstandarderror = true;              //create output , streamreader output             string output = null; streamreader sroutput = null;              //try process             try             {                 //run process                 process proc = system.diagnostics.process.start(oinfo);                  proc.waitforexit();                  //get output                 sroutput = proc.standarderror;                  //now put in string                 output = sroutput.readtoend();                  proc.close();             }             catch (exception)             {                 output = string.empty;             }                         {                 //now, if succeded, close out streamreader                 if (sroutput != null)                 {                     sroutput.close();                     sroutput.dispose();                 }             }             return output;         }         #endregion          #region getvideoinfo         public videofile getvideoinfo(memorystream inputfile, string filename)         {             string tempfile = path.combine(this.workingpath, system.guid.newguid().tostring() + path.getextension(filename));             filestream fs = file.create(tempfile);             inputfile.writeto(fs);             fs.flush();             fs.close();             gc.collect();              videofile vf = null;             try             {                 vf = new videofile(tempfile);             }             catch (exception ex)             {                 throw ex;             }              getvideoinfo(vf);              try             {                 file.delete(tempfile);             }             catch (exception)             {              }              return vf;         }         public videofile getvideoinfo(string inputpath)         {             videofile vf = null;             try             {                 vf = new videofile(inputpath);             }             catch (exception ex)             {                 throw ex;             }             getvideoinfo(vf);             return vf;         }         public void getvideoinfo(videofile input)         {             //set parameters video info             string params = string.format("-i {0}", input.path);             string output = runprocess(params);             input.rawinfo = output;              //get duration             regex re = new regex("[d|d]uration:.((\\d|:|\\.)*)");             match m = re.match(input.rawinfo);              if (m.success)             {                 string duration = m.groups[1].value;                 string[] timepieces = duration.split(new char[] { ':', '.' });                 if (timepieces.length == 4)                 {                     input.duration = new timespan(0, convert.toint16(timepieces[0]), convert.toint16(timepieces[1]), convert.toint16(timepieces[2]), convert.toint16(timepieces[3]));                 }             }              //get audio bit rate             re = new regex("[b|b]itrate:.((\\d|:)*)");             m = re.match(input.rawinfo);             double kb = 0.0;             if (m.success)             {                 double.tryparse(m.groups[1].value, out kb);             }             input.bitrate = kb;              //get audio format             re = new regex("[a|a]udio:.*");             m = re.match(input.rawinfo);             if (m.success)             {                 input.audioformat = m.value;             }              //get video format             re = new regex("[v|v]ideo:.*");             m = re.match(input.rawinfo);             if (m.success)             {                 input.videoformat = m.value;             }              //get video format             re = new regex("(\\d{2,3})x(\\d{2,3})");             m = re.match(input.rawinfo);             if (m.success)             {                 int width = 0; int height = 0;                 int.tryparse(m.groups[1].value, out width);                 int.tryparse(m.groups[2].value, out height);                 input.width = width;                 input.height = height;             }             input.infogathered = true;         }         #endregion          #region convert flv         public outputpackage converttoflv(memorystream inputfile, string filename)         {             string tempfile = path.combine(this.workingpath, system.guid.newguid().tostring() + path.getextension(filename));             filestream fs = file.create(tempfile);             inputfile.writeto(fs);             fs.flush();             fs.close();             gc.collect();              videofile vf = null;             try             {                 vf = new videofile(tempfile);             }             catch (exception ex)             {                 throw ex;             }              outputpackage oo = converttoflv(vf);              try             {                 file.delete(tempfile);             }             catch (exception)             {              }              return oo;         }         public outputpackage converttoflv(string inputpath)         {             videofile vf = null;             try             {                 vf = new videofile(inputpath);             }             catch (exception ex)             {                 throw ex;             }              outputpackage oo = converttoflv(vf);             return oo;         }         public outputpackage converttoflv(videofile input)         {             if (!input.infogathered)             {                 getvideoinfo(input);             }             outputpackage ou = new outputpackage();              //set parameters getting previewimage             string filename = system.guid.newguid().tostring() + ".jpg";             int secs;              //divide duration in 3 preview image in middle of clip             //instead of black image beginning.             secs = (int)math.round(timespan.fromticks(input.duration.ticks / 3).totalseconds, 0);              string finalpath = path.combine(this.workingpath, filename);             string params = string.format("-i {0} {1} -vcodec mjpeg -ss {2} -vframes 1 -an -f rawvideo", input.path, finalpath, secs);             string output = runprocess(params);              ou.rawoutput = output;              if (file.exists(finalpath))             {                 ou.previewimage = loadimagefromfile(finalpath);                 try                 {                     file.delete(finalpath);                 }                 catch (exception) { }             }             else             { //try running again @ frame 1                 params = string.format("-i {0} {1} -vcodec mjpeg -ss {2} -vframes 1 -an -f rawvideo", input.path, finalpath, 1);                 output = runprocess(params);                  ou.rawoutput = output;                  if (file.exists(finalpath))                 {                     ou.previewimage = loadimagefromfile(finalpath);                     try                     {                         file.delete(finalpath);                     }                     catch (exception) { }                 }             }              finalpath = path.combine(this.workingpath, filename);             filename = system.guid.newguid().tostring() + ".flv";             params = string.format("-i {0} -y -ar 22050 -ab 64 -f flv {1}", input.path, finalpath);             output = runprocess(params);              if (file.exists(finalpath))             {                 ou.videostream = loadmemorystreamfromfile(finalpath);                 try                 {                     file.delete(finalpath);                 }                 catch (exception) { }             }             return ou;         }         #endregion     }      public class videofile     {         #region properties         private string _path;         public string path         {                         {                 return _path;             }             set             {                 _path = value;             }         }          public timespan duration { get; set; }         public double bitrate { get; set; }         public string audioformat { get; set; }         public string videoformat { get; set; }         public int height { get; set; }         public int width { get; set; }         public string rawinfo { get; set; }         public bool infogathered { get; set; }         #endregion          #region constructors         public videofile(string path)         {             _path = path;             initialize();         }         #endregion          #region initialization         private void initialize()         {             this.infogathered = false;             //first make sure have value video file setting             if (string.isnullorempty(_path))             {                 throw new exception("could not find location of video file");             }              //now see if video file exists             if (!file.exists(_path))             {                 throw new exception("the video file " + _path + " not exist.");             }         }         #endregion     }      public class outputpackage     {         public memorystream videostream { get; set; }         public system.drawing.image previewimage { get; set; }         public string rawoutput { get; set; }         public bool success { get; set; }     } } 

this log:

4/20/2013--5:32 pm ==>  4/20/2013--6:47 pm ==> ffmpeg version n-52233-gee94362 copyright (c) 2000-2013 ffmpeg developers 4/20/2013--6:47 pm ==>   built on apr 18 2013 02:55:39 gcc 4.8.0 (gcc) 4/20/2013--6:47 pm ==>   configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib 4/20/2013--6:47 pm ==>   libavutil      52. 26.100 / 52. 26.100 4/20/2013--6:47 pm ==>   libavcodec     55.  2.100 / 55.  2.100 4/20/2013--6:47 pm ==>   libavformat    55.  2.100 / 55.  2.100 4/20/2013--6:47 pm ==>   libavdevice    55.  0.100 / 55.  0.100 4/20/2013--6:47 pm ==>   libavfilter     3. 56.103 /  3. 56.103 4/20/2013--6:47 pm ==>   libswscale      2.  2.100 /  2.  2.100 4/20/2013--6:47 pm ==>   libswresample   0. 17.102 /  0. 17.102 4/20/2013--6:47 pm ==>   libpostproc    52.  3.100 / 52.  3.100 4/20/2013--6:47 pm ==> input #0, avi, 'd:\testdata\new.avi': 4/20/2013--6:47 pm ==>   duration: 00:00:02.12, start: 0.000000, bitrate: 1658893 kb/s 4/20/2013--6:47 pm ==>     stream #0:0: video: rawvideo, bgra, 1920x1080, 25 tbr, 25 tbn, 25 tbc 4/20/2013--6:47 pm ==> codec avoption b (set bitrate (in bits/s)) specified output file #0 (d:\testdata\new.flv) has not been used stream. reason either wrong type (e.g. video option no video streams) or private option of encoder not used stream. 4/20/2013--6:47 pm ==> output #0, flv, 'd:\testdata\new.flv': 4/20/2013--6:47 pm ==>   metadata: 4/20/2013--6:47 pm ==>     encoder         : lavf55.2.100 4/20/2013--6:47 pm ==>     stream #0:0: video: mpeg4 ([9][0][0][0] / 0x0009), yuv420p, 1920x1080, q=2-31, 200 kb/s, 1k tbn, 25 tbc 4/20/2013--6:47 pm ==> stream mapping: 4/20/2013--6:47 pm ==>   stream #0:0 -> #0:0 (rawvideo -> mpeg4) 4/20/2013--6:47 pm ==> press [q] stop, [?] 4/20/2013--6:47 pm ==> frame=    8 fps=0.0 q=18.4 size=     599kb time=00:00:00.32 bitrate=15326.9kbits/s     4/20/2013--6:47 pm ==> frame=   14 fps= 12 q=31.0 size=     669kb time=00:00:00.56 bitrate=9779.3kbits/s     4/20/2013--6:47 pm ==> frame=   20 fps= 12 q=31.0 size=     680kb time=00:00:00.80 bitrate=6966.3kbits/s     4/20/2013--6:47 pm ==> frame=   24 fps= 11 q=31.0 size=     688kb time=00:00:00.96 bitrate=5872.3kbits/s     4/20/2013--6:47 pm ==> frame=   31 fps= 11 q=31.0 size=     764kb time=00:00:01.24 bitrate=5044.3kbits/s     4/20/2013--6:47 pm ==> frame=   37 fps= 11 q=24.8 size=     853kb time=00:00:01.48 bitrate=4723.3kbits/s     4/20/2013--6:47 pm ==> frame=   42 fps= 11 q=31.0 size=     859kb time=00:00:01.68 bitrate=4190.6kbits/s     4/20/2013--6:47 pm ==> frame=   48 fps= 11 q=31.0 size=     885kb time=00:00:01.92 bitrate=3777.2kbits/s     4/20/2013--6:47 pm ==> frame=   53 fps= 11 q=31.0 size=     956kb time=00:00:02.12 bitrate=3695.0kbits/s     4/20/2013--6:47 pm ==> frame=   53 fps= 11 q=31.0 lsize=     956kb time=00:00:02.12 bitrate=3695.1kbits/s     4/20/2013--6:47 pm ==>  4/20/2013--6:47 pm ==> video:955kb audio:0kb subtitle:0 global headers:0kb muxing overhead 0.135293% 4/20/2013--6:47 pm ==>  4/20/2013--6:47 pm ==>  


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -