Вы находитесь на странице: 1из 6


A Video Converter in Visual Basic .NET - CodeProject

Not quite what

ou are looking for? You ma

want to tr :
highligh off

DirectShow VB.NET Example nVLC

8,397,374 membe and g o ing! Email Password Sign in Join

Remember me? Lost password?


A icle

Q ick An

Di c


Lea ning Zone


C POL 13 J n 2011 5,861 1,214 8 ime

The Lounge

ideo call VB



A Video Con e e in Vi .NET

By Olabenjo T nde | 13 Jun 2011

al Ba ic

Licence First Posted Views Downloads Bookmarked

See Al o
More like this More by this author

VB Windows Win32 Win64 Visual-Studio Beginner Intermediate Advanced Multimedia C ode

This is a simple video converter I developed with .NET while playing with ffmpeg.
A icle B o e Code S a Re i ion (6)
1.00 (1 vote) 5

Spon o ed Link
Step-By-Step WC F (Windows C ommunication Foundation) Video If you want to learn WC F in one quick and easy lesson, then this video... www.pdsa.com Banckle Enterprise IM Banckle Enterprise IM is a completely secure business collaboration... banckle.com

Download source - 2.45 MB

In od c ion
This is a simple ideo converter developed with .NET while playing with ffmpeg. Ffmpeg is an open source command line audio / ideo converter. ffmpeg uses command line arguments for its conversion and this is what we are going to do with our .NET application. We will execute ffmpeg from our application and send these parameters to ffmpeg without displaying the command line window. Download the source code here to see how it works.

Backg o nd
The application uses a background worker. B c g o n W r e prevents our form from akrudokr hanging while the conversion is in progress. I had that problem whenever I started the ffmpeg process. The B c g o n W r e component gives you the ability to execute time-consuming akrudokr operations asynchronously ("in the background"), on a thread different from your application's main UI thread. To use a B c g o n W r e , you simply tell it what time-consuming worker akrudokr method to execute in the background, and then you call the R n o k r s n method. Your uWreA c calling thread continues to run normally while the worker method runs asynchronously. When the method is finished, the B c g o n W r e alerts the calling thread by firing the akrudokr R n o k r o p e e event, which optionally contains the results of the operation. (MSDN uWreCmltd 2008) Ffmpeg is freely available on the internet. You need to have the binary version, i.e., ffmpeg.e e in the application directory of your project. It is included in the zip file.


o C ea e he Con e e

Let's get started by using ffmpeg.e e. Open the command prompt in your Windows environment and navigate to the directory where ffmpeg.e e is located. Now let's say ffmpeg is located in C:\ i.e., C:\ffmpeg.e e, we navigate to our C: directory on the command prompt and type in a few commands into ffmpeg. Let's say we have a .avi ideo file in our C: directory and want to convert that ideo to a .flv file, we just need to enter the command:
C ollapse | C opy C ode

fme - iptaiotu.l fpg i nu.v uptfv

Note that different arguments can be parsed to ffmpeg for various kinds on conversion, but for
www.codeproject.com/KB/vb/videoConverter.aspx 1/6


A Video Converter in Visual Basic .NET - CodeProject

the purpose of this tutorial, we will stick to the very basic commands needed to convert our video file. What that argument above does is that it creates a converted video version of the file input.avi to output.flv in our C: directory. Basically, this is what our program is going to do but this time around without the command prompt. We will be sending the arguments through our program to ffmpeg. For more documentation on how to use ffmpeg, checkout this FFmpeg site... Now let's begin with our program in .NET.

Step 1
Insert three textboxes, a trackbar, a background worker, open and save dialog boxes and four command buttons into your new form. You also need to place ffmpeg.e e in the /bin folder of your application directory.

See Also...
DirectShow VB.NET Example nVLC

The d a g a eand d a g e tools enable us to specify where our file is located and i S i O where we want to save the output file. The b c g a d W e tool allows us to run the ffmpeg process in a different thread from our application's UI thread, this allows our application to run properly without interruption from ffmpeg process while the actual conversion is in progress.

Problems in the AudioVideoPlayback namespace of managed DirectX9 C # \ VB.NET C amera C ommunication Libraries DirectShow.NET DFB vs. DIB Play Your Avi Files with this DirectX Video Player

Step 2
Let's create a function that performs the conversion:
C ollapse C opy C ode

C amera Vision - video surveillance on C # VideoTexture C lass and Webcam Application for XNA Transparent Desktop Video Downloading Youtube Videos C # WinForm Using the DirectShow Video Mixing Renderer 9 filter Silverlight Advanced View Model Style Video Player Authoring Visual Studio Debugger Visualizers Manage YouTube using C # and Youtube API 1.6 DirectShow MediaPlayer in C #

F ci

a C e i ( ) C .hcF I eaC Ce g T eda h aC =F e a D i i A S ig=M. g e.ieae'h iptfl ed O F N te nu ie D i A S ig=M. gaeF eae'h otu fl ed S .i N te upt ie 'fpglcto fme oain D eeahA S ig=A iai . a i c S P h+"b \f e. e a \i f ge " D i ai A I ee =Ta B 1V e*2 g c a .a D i a if A N S e.ig e Da i . c Sa If c P e D i A S e Rae a ed D cdA S ig="- ""+i i i " +""- 200- c e"_ " a 25 a & ai &"- ""+ " +"" "" 'fpgcmad - t freoewie fme omns y o oc vrrt 'te qcl oto alw u t stteqaiyo or ie wt ortaka h sae pin los s o e h ult f u do ih u rcbr D f eO i f g A S ig

The Dail Insider

30 free programming books Daily News: Signup now.

'l prmtr rqie t rntepoes al aaees eurd o u h rcs 'fpgue sadr errt dslyisotu fme ss tnad ro o ipa t upt



a a a a a

A Video Converter in Visual Basic .NET - CodeProject

if.ieae=eeah F N if. g e A =cd if. eh Ee e=F e U Se c a if.id S e=P c W d S eHde W e i .id if.eie Sad d Rd c a E =T e 'eietfmesotu rdrc fpg upt 'oorapiain t u plcto a if.eie Sad d Rd c a O =T e'edntral ne ti w o ely ed hs a if. e e W d =T e C a N i cSa If = a if . cSa ( 'sattepoes . ) tr h rcs M.bIf.e =" e T C e i i ge ..Pe e a .. . a i ." = cSad d . a E 'tnaderri ue b fme sadr ro s sd y fpg M. Sa . a e =F e eb E b d a D I Bcg f a d W e1C c ai P d gTe .a e e i h 'hc i acnelto rqetwsmd ad cek f aclain eus a ae n 'xttefnto i tu ei h ucin f re Ei F ci EdI f f eO f g = .edie 'ipasfme sotu RaL dsly fpg upt 'ntetxbxoeln a atm i h eto n ie t ie M. P ge .e =f eO e T f g L U i cH Eie Adf eO .a d f g =N h gO f eO i f g M. P ge .e ="iihd! e T F e " M.bIf.e =" e T C ee! d" Mg (C B " ee!,Mg S eEcaai ) d" B . M. Sa . a e =T e eb E b d R e 0 EdF ci

=" "

We created a function with ffmpeg.

a C

e i ( which performs the actual conversion of the file )

Setting C .hcF I eaC Ce g T eda h aC to F a eprevents Visual Studio from catching calls on a different thread that accesses a control's H d e property when an a application is being debugged because we will be calling our controls from another thread separate from our form s thread, i.e., the b c g a d W e process where our function is located.
C ollapse | C opy C ode

D cdA S ig="- ""+i i i " +""- 200- c e"_ " a 25 a & ai &"- ""+ " +"" ""

This command is parsed to ffmpeg.e e to do the actual conversion for us. "- i i " specifies our input file, e.g., sample.avi, the "a 2 0 0 specifies the audio 25" sampling frequency as 22050 which is common for flash ideo files, we can also specify a different one like 44100 which is actually the default in ffmpeg. "- c e specifies the quality a " of the ideo e.g.,. "- c e 2 is of higher quality than "- c e 1 ". The value of our a " a 0 quality variable is retrieved from the trackbar. We want our users to specify the quality of the ideo they want using the trackbar. Note that a i f contains properties that allows us to specify that our process is being run. The W d S i eproperty is set to hidden, this prevents the command prompt console from showing. property is used to redirect whatever is displayed on the command prompt to a different control. But ffmpeg uses ad d a E to display its conversion output, this is why we have to use the R d e S a d d ei c a E and set its value to e .

Rd e Sad d ei c a O

d W e 1 C c a i P d gshows that a cancellation request was sent .a e e i to terminate the process, we use this property to exit our function, this actually kills the ffmpeg process. Bcg a
A textbox f f eO g . e d i eto it. RaL is used to display the output from ffmpeg by assigning

Step 3
On the

a B

, insert the following code:

C ollapse | C opy C ode




A Video Converter in Visual Basic .NET - CodeProject

PiaeSbbntr_lc(ya sne A Sse.bet _ rvt u tSatCikBVl edr s ytmOjc, BVleA Sse.vnAg)Hnlsbntr.lc ya s ytmEetrs ade tSatCik I ttpnTx =" O ttpnTx < dgpnFlNm Te f xOe.et " r xOe.et > lOe.ieae hn Mgo(Slc afl t cnet,MgoSyeIfrain "eetafl" sBx"eet ie o ovr" sBxtl.nomto, Slc ie) Ei Sb xt u EsI ttaeTx =" O ttaeTx < dgaeFlNm Te lef xSv.et " r xSv.et > lSv.ieae hn Mgo(Slc yu otu flnm" MgoSyeIfrain "eetafl" sBx"eet or upt ieae, sBxtl.nomto, Slc ie) Ei Sb xt u EdI n f BcgonWre1Rnokrsn( sattebcgon wre akrudokr.uWreAyc) tr h akrud okr EdSb n u

R n o k r s n ( action of the b c g o n W r e is used to start the process. What uWreAyc) akrudokr this does is that it executes the s a t o v r i n )function we inserted in the trCneso( B c g o n W r e 1 D W r procedure. akrudokr_ook Step 4
On the save dialog tool, insert the following code:
C ollapse | C opy C ode

PiaeSbdgaeFlO(ya sne A Sse.bet _ rvt u lSv_iekBVl edr s ytmOjc, BVleA Sse.opnnMdlCneEetrs HnlsdgaeFlO ya s ytmCmoetoe.aclvnAg) ade lSv.iek dgaeOewiermt=Tu lSv.vrrtPop re dgaeDrfrneik =Tu lSv.eeeecLns re dgaeCetPop =Tu lSv.raermt re dgaeDfutx ="fv lSv.ealEt .l" 'h po a cne i g m o of ol, l n 'o cnal a lo h e e o aei a n n 'om b ic dn m e ie fl f a nl ig o do ie ' e ini h aeo indao b e n o n e p o ilg o ttaeTx =dgaeFlNm xSv.et lSv.ieae EdSb n u 'o o h cne inpoe , e edan iiain o h T p e o o c n o fc o e 'ak onW k adkl h fme poe bcg do e n il e fpg c PiaeSbbntpCikBVlsne A Sse.bet _ rvt u tSo_lc(ya edr s ytmOjc, BVleA Sse.vnAg)HnlsbntpCik ya s ytmEetrs ade tSo.lc O ErrGT hnl n ro oo ade BcgonWre1CneAyc) akrudokr.aclsn( I bntr.nbe =FleTe f tSatEald as hn llnoTx =(Cneso Cnee!) bIf.et "ovrin acld" Mgo(Cneso hsbe cneld" MgoSyeEcaain sBx"ovrin a en acle!, sBxtl.xlmto) bntr.nbe =Tu tSatEald re Es le Mgo(Satcneso frt,MgoSyeCiia) sBx"tr ovrin is" sBxtl.rtcl EdI n f po.il) rcKl( hnl: ade Ei Sb xt u EdSb n u

On our stop button, we send a C n e A y c )request to the b c g o n W r e . This aclsn( akrudokr request enables us to handle anything we need to display or do before we end the process. For example, we can display a message box that confirms cancellation and also perform a different task on the process before the process ends.

Step 5
The b c g o n W r e calls the conversion function here: akrudokr
C ollapse | C opy C ode

PiaeSbBcgonWre1DWr(ya sne A Sse.bet _ rvt u akrudokr_ookBVl edr s ytmOjc, BVleA Sse.opnnMdlDWrEetrs HnlsBcgonWre1DWr ya s ytmCmoetoe.ookvnAg) ade akrudokr.ook satovrin) trCneso( EdSb n u

Points of Interest
This application only converts different ideo files to .fl (Flash ideo). To save your ideos in any other format, simply add more ideo file extensions to the save dialog box filter property. I hope you ll have fun with this.
www.codeproject.com/KB/vb/videoConverter.aspx 4/6


A Video Converter in Visual Basic .NET - CodeProject

J he fi applica ion. Rega d , T nde Olabenjo

Thi a icle, along i h an a P ojec Open Licen e (CPOL) ocia ed o ce code and file , i licen ed nde The Code

About the Author

Olabenjo Tunde
C EO EdgeWalk Nige ia Membe Follo on T i e

S mma CCNA MCSA MCSA Sec i Sec i + MCITP Se e Admini a o MCTS SQL Se e 2005 MCTS Windo 2008 Ne o k Inf a c e MCTS ISA e e 2006 MCTS Ac i e di ec o Se e 08 Special ie VB.Ne , J2Me, PHP(F ame o kCodeIgni e ), Ja a, C++, SQL, HTML5, CSS(ba ic), XML, AJAX(ba ic), jQ e , ja a c ip , RESTf l Web Se ice and API, V lne abili a e men , Pene a ion e ing, mobile applica ion de elopmen (phonegap, ne bean , Q ).

A icle Top

Sign Up o



E cellent Vo e

Comments and Discussions

You must Sign In to use this message board. ( ec FAQ P ofile pop p Ref e h File converts but si e is 0 how to use the code in wpf application ffmpeg hangs for larger files Permission Re: Pe mi La ion La Upda e: 11:36 10 Jan '12 terrancefisher abha 004 Chris Quinn Benjamin Unanka Olabenjo T nde Noi e le el Medi m La o

e ign-in) Search Pe page 25 Fi Upda e P e Ne

2:58 21 Dec '11 22:11 14 Sep '11 4:45 13 Jun '11 3:15 13 Jun '11 3:41 13 J n '11 1

Vi i : 19:00 31 Dec '99



General Admin News Suggestion

A Video Converter in Visual Basic .NET - CodeProject

Question Bug Answer Joke Rant

Use C trl+Left/Right to switch messages, C trl+Up/Down to switch threads, C trl+Shift+Left/Right to switch pages.
P ermalink | A dvertis e | P rivac y | M obile L ayout: fixed | Web0 3 | 2 .5 .1 2 0 1 0 9 .1 | L as t U pdated 1 3 J un 2 0 1 1 fluid A rtic le C opyright 2 0 1 1 by O labenjo T unde E verything els e C opyright C odeP rojec t, 1 9 9 9 - 2 0 1 2 T erms of U s e