Click here to Skip to main content
15,885,032 members
Articles / Web Development / ASP.NET
Article

Converting , extracting preview image in asp.net via ffmpeg

Rate me:
Please Sign up or sign in to vote.
2.47/5 (21 votes)
22 Jun 2008CPOL10 min read 137.1K   2.9K   71   29
how to use ffmpeg

Introduction

Well in this article I’ll talk about how using ffmpeg with asp.net , for converting video , extracting preview image and ….

There are three way to using ffmpeg in your website:

Using sharpffmpeg ( http://sourceforge.net/projects/sharpffmpeg/ )

Using ffmpeg dll

Using ffmpeg execution

The last one is easiest one and in this article I’ll explain this way.

Why ffmpeg

First of all I dunno any other way for extracting a preview frame of 3gp files. ( but there are other ways for converting videos)

And Also ffmpeg support following formats :

2.1 File Formats

FFmpeg supports the following file formats through the libavformat library:

Supported File Format Encoding Decoding Comments
MPEG audio X X
MPEG-1 systems X Xmuxed audio and video
MPEG-2 PS X Xalso known as VOB file
MPEG-2 TS Xalso known as DVB Transport Stream
ASFX X
AVIX X
WAVX X
Macromedia FlashX XOnly embedded audio is decoded.
FLV X XMacromedia Flash video files
Real Audio and Video X X
Raw AC3 X X
Raw MJPEG X X
Raw MPEG video X X
Raw PCM8/16 bits, mulaw/AlawX X
Raw CRI ADX audio X X
Raw Shorten audio X
SUN AU format X X
NUT X X NUT Open Container Format
QuickTime X X
MPEG-4 X XMPEG-4 is a variant of QuickTime.
Raw MPEG4 video X X
DV X X
4xm X4X Technologies format, used in some games.
Playstation STR X
Id RoQ X XUsed in Quake III, Jedi Knight 2, other computer games.
Interplay MVE XFormat used in various Interplay computer games.
WC3 Movie XMultimedia format used in Origin's Wing Commander III computer game.
Sega FILM/CPK XUsed in many Sega Saturn console games.
Westwood Studios VQA/AUD XMultimedia formats used in Westwood Studios games.
Id Cinematic (.cin) XUsed in Quake II.
FLIC format X.fli/.flc files
Sierra VMD XUsed in Sierra CD-ROM games.
Sierra Online X.sol files used in Sierra Online games.
Matroska X X
Electronic Arts Multimedia XUsed in various EA games; files have extensions like WVE and UV2.
Nullsoft Video (NSV) format X
ADTS AAC audio X X
Creative VOC X X Created for the Sound Blaster Pro.
American Laser Games MM XMultimedia format used in games like Mad Dog McCree
AVS XMultimedia format used by the Creature Shock game.
Smacker XMultimedia format used by many games.
GXF X XGeneral eXchange Format SMPTE 360M, used by Thomson Grass Valley playout servers.
CIN XMultimedia format used by Delphine Software games.
MXF XMaterial eXchange Format SMPTE 377M, used by D-Cinema, broadcast industry.
SEQ XTiertex .seq files used in the DOS CDROM version of the game Flashback.
DXA XThis format is used in non-Windows version of Feeble Files game and different game cutscenes repacked for use with ScummVM.
THP XUsed on the Nintendo GameCube.
C93 XUsed in the game Cyberia from Interplay.
Bethsoft VID XUsed in some games from Bethesda Softworks.
CRYO APC XAudio format used in some games by CRYO Interactive Entertainment.

X means that encoding (resp. decoding) is supported.

2.2 Image Formats

FFmpeg can read and write images for each frame of a video sequence. The following image formats are supported:

Supported Image Format Encoding Decoding Comments
PGM, PPM X X
PAM X X PAM is a PNM extension with alpha support.
PGMYUV X X PGM with U and V components in YUV 4:2:0
JPEG X X Progressive JPEG is not supported.
.Y.U.V X X one raw file per component
animated GIF X X Only uncompressed GIFs are generated.
PNG X X 2 bit and 4 bit/pixel not supported yet.
Targa X Targa (.TGA) image format.
TIFF X X YUV, JPEG and some extension is not supported yet.
SGI X X SGI RGB image format
PTX X V.Flash PTX format

X means that encoding (resp. decoding) is supported.

2.3 Video Codecs

Supported Codec Encoding Decoding Comments
MPEG-1 video X X
MPEG-2 video X X
MPEG-4 X X
MSMPEG4 V1 X X
MSMPEG4 V2 X X
MSMPEG4 V3 X X
WMV7 X X
WMV8 X X not completely working
WMV9 X not completely working
VC1 X
H.261 X X
H.263(+) X X also known as RealVideo 1.0
H.264 X
RealVideo 1.0 X X
RealVideo 2.0 X X
MJPEG X X
lossless MJPEG X X
JPEG-LS X X fourcc: MJLS, lossless and near-lossless is supported
Apple MJPEG-B X
Sunplus MJPEG X fourcc: SP5X
DV X X
HuffYUV X X
FFmpeg Video 1 X X experimental lossless codec (fourcc: FFV1)
FFmpeg Snow X X experimental wavelet codec (fourcc: SNOW)
Asus v1 X X fourcc: ASV1
Asus v2 X X fourcc: ASV2
Creative YUV X fourcc: CYUV
Sorenson Video 1 X X fourcc: SVQ1
Sorenson Video 3 X fourcc: SVQ3
On2 VP3 X still experimental
On2 VP5 X fourcc: VP50
On2 VP6 X fourcc: VP60,VP61,VP62
Theora X X still experimental
Intel Indeo 3 X
FLV X X Sorenson H.263 used in Flash
Flash Screen Video X X fourcc: FSV1
ATI VCR1 X fourcc: VCR1
ATI VCR2 X fourcc: VCR2
Cirrus Logic AccuPak X fourcc: CLJR
4X Video X Used in certain computer games.
Sony Playstation MDEC X
Id RoQ X X Used in Quake III, Jedi Knight 2, other computer games.
Xan/WC3 X Used in Wing Commander III .MVE files.
Interplay Video X Used in Interplay .MVE files.
Apple Animation X X fourcc: 'rle '
Apple Graphics X fourcc: 'smc '
Apple Video X fourcc: rpza
Apple QuickDraw X fourcc: qdrw
Cinepak X
Microsoft RLE X
Microsoft Video-1 X
Westwood VQA X
Id Cinematic Video X Used in Quake II.
Planar RGB X fourcc: 8BPS
FLIC video X
Duck TrueMotion v1 X fourcc: DUCK
Duck TrueMotion v2 X fourcc: TM20
VMD Video X Used in Sierra VMD files.
MSZH X Part of LCL
ZLIB X X Part of LCL, encoder experimental
TechSmith Camtasia X fourcc: TSCC
IBM Ultimotion X fourcc: ULTI
Miro VideoXL X fourcc: VIXL
QPEG X fourccs: QPEG, Q1.0, Q1.1
LOCO X
Winnov WNV1 X
Autodesk Animator Studio Codec X fourcc: AASC
Fraps FPS1 X
CamStudio X fourcc: CSCD
American Laser Games Video X Used in games like Mad Dog McCree
ZMBV X X Encoder works only on PAL8
AVS Video X Video encoding used by the Creature Shock game.
Smacker Video X Video encoding used in Smacker.
RTjpeg X Video encoding used in NuppelVideo files.
KMVC X Codec used in Worms games.
VMware Video X Codec used in videos captured by VMware.
Cin Video X Codec used in Delphine Software games.
Tiertex Seq Video X Codec used in DOS CDROM FlashBack game.
DXA Video X Codec originally used in Feeble Files game.
AVID DNxHD X aka SMPTE VC3
C93 Video X Codec used in Cyberia game.
THP X Used on the Nintendo GameCube.
Bethsoft VID X Used in some games from Bethesda Softworks.
Renderware TXD X Texture dictionaries used by the Renderware Engine.

X means that encoding (resp. decoding) is supported.

2.4 Audio Codecs

Supported Codec Encoding Decoding Comments
MPEG audio layer 2 IX IX
MPEG audio layer 1/3 IX IXMP3 encoding is supported through the external library LAME.
AC3 IX IXliba52 is used internally for decoding.
Vorbis X X
WMA V1/V2 X X
AAC X XSupported through the external library libfaac/libfaad.
Microsoft ADPCM X X
MS IMA ADPCM X X
QT IMA ADPCM X
4X IMA ADPCM X
G.726 ADPCM X X
Duck DK3 IMA ADPCM XUsed in some Sega Saturn console games.
Duck DK4 IMA ADPCM XUsed in some Sega Saturn console games.
Westwood Studios IMA ADPCM XUsed in Westwood Studios games like Command and Conquer.
SMJPEG IMA ADPCM XUsed in certain Loki game ports.
CD-ROM XA ADPCM X
CRI ADX ADPCM X XUsed in Sega Dreamcast games.
Electronic Arts ADPCM XUsed in various EA titles.
Creative ADPCM X16 -> 4, 8 -> 4, 8 -> 3, 8 -> 2
THP ADPCM XUsed on the Nintendo GameCube.
RA144 XReal 14400 bit/s codec
RA288 XReal 28800 bit/s codec
RADnet X IXReal low bitrate AC3 codec, liba52 is used for decoding.
AMR-NB X XSupported through an external library.
AMR-WB X XSupported through an external library.
DV audio X
Id RoQ DPCM X XUsed in Quake III, Jedi Knight 2, other computer games.
Interplay MVE DPCM XUsed in various Interplay computer games.
Xan DPCM XUsed in Origin's Wing Commander IV AVI files.
Sierra Online DPCM XUsed in Sierra Online game audio files.
Apple MACE 3 X
Apple MACE 6 X
FLAC lossless audio X X
Shorten lossless audio X
Apple lossless audio XQuickTime fourcc 'alac'
FFmpeg Sonic X Xexperimental lossy/lossless codec
Qdesign QDM2 Xthere are still some distortions
Real COOK XAll versions except 5.1 are supported
DSP Group TrueSpeech X
True Audio (TTA) X
Smacker Audio X
WavPack Audio X
Cin Audio XCodec used in Delphine Software games.
Intel Music Coder X
Musepack XOnly SV7 is supported
DT$ Coherent Audio X
ATRAC 3 X

X means that encoding (resp. decoding) is supported. I means that an integer-only version is available, too (ensures high performance on systems without hardware floating point support).

Using the code

First of all go to this url ( http://arrozcru.no-ip.org/ffmpeg_builds/ ) and download latest ffmpeg builds.

For using sample of this article you’ll need to download ffmpeg from above link and put

“ffmpeg.exe” , “pthreadGC2.dll” , “SDL.dll” in sample project folder .

notice : you have to put a 3gp video and you must rename it to first.3gp in project folder or also you can change code for using other videos instead of first.3gp .

Code analyze

enclosed sample is straightforward and simple but we cover some part of it.

First of all you need to add System.Diagnostics name space to your project ( for accessing to process class )

C#
         // preview frame



Process ffmpeg; // creating process


string video;
string thumb;
video = Page.MapPath("first.3gp"); // setting video input name with path


thumb = Page.MapPath("") +"\\frame.jpg"; // thumb name with path !


ffmpeg = new Process();

ffmpeg.StartInfo.Arguments = " -i \""+video+"\" -s 108*80  -vframes 1 -f image2 -vcodec mjpeg \""+thumb+"\""; // arguments !


ffmpeg.StartInfo.FileName = Page.MapPath("ffmpeg.exe");
ffmpeg.Start(); // start ! 

The code above is used to extracting a preview image from 3gp video.

You see that we have 2 variables , video and thumb that name of both is clear

Video variable hold video which we want to extracting preview thumbnail or image from ( usually the video which user uploaded )

And thumb hold the name that we want to call the output file.

We combine them and send them as arguments to ffmpeg. these special arguments tell ffmpeg create image with 180*80 resolution , you can change each part of it .

Now let’s look at the code behind convert button in our project :

C#
//converting video 



        Process ffmpeg; // creating process


        string video;
        string mpg;
        video = Page.MapPath("first.3gp"); // setting video input name with path


        mpg = Page.MapPath("") + "\\video.mpg"; // thumb name with path !


        ffmpeg = new Process();

        ffmpeg.StartInfo.Arguments = " -i \"" + video + "\" -target vcd \"" + mpg + "\""; // arguments !


        ffmpeg.StartInfo.FileName = Page.MapPath("ffmpeg.exe");
        ffmpeg.Start(); // start ! 

You see that all thing are the same just the name of variable of output file changed and now we call it mpg and the arguments of ffmpeg also changed.

You see it’s really easy. But please consider that converting videos is so heavy process, you must have good reason to letting user to converting video on your web site.

Conclusion

Thats all, I hope you enjoyed of reading this article and find it useful and please consider that it was my first article.

If you have your own server you don’t have problem and surely you have permission for executing file

But if you use shared web server ( like me ! ) coz ffmpeg is well know open source project you can talk to your host manager and giving information about ffmpeg and its usage or giving its url

Maybe in future I write about other way.

License

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


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
Sadegh hosseini is system programmer and web developer
He worked with many language such as c# , java , delphi , php

He is also interested in philosophy,electronic and music
and founder of two open source projects

http://sourceforge.net/projects/theida |open source ide

http://sourceforge.net/projects/mida | mobile version of above projects


doremond.com


bia2goshi.com

Comments and Discussions

 
GeneralGreate Article Pin
GPUToaster™25-Jun-10 0:19
GPUToaster™25-Jun-10 0:19 
GeneralMy vote of 1 Pin
lavoryk28-Jan-10 22:36
lavoryk28-Jan-10 22:36 
GeneralMy vote of 1 Pin
Qiang He4-Dec-09 20:39
Qiang He4-Dec-09 20:39 
GeneralMy vote of 5 (Excelent) Pin
Vuvirt1-Dec-09 9:36
Vuvirt1-Dec-09 9:36 
Generaladd watermark to flv video by using ffmpeg on windows. Pin
HuyVo1-Nov-09 17:16
HuyVo1-Nov-09 17:16 
GeneralMy vote of 1 Pin
Christian Kleinheinz26-Oct-09 5:52
Christian Kleinheinz26-Oct-09 5:52 
GeneralCheck out this if u want to do without exe. Pin
Jahangir Shahzad9-Apr-09 20:15
Jahangir Shahzad9-Apr-09 20:15 
Generalwrong specifications of -vframes Pin
Jahangir Shahzad9-Apr-09 20:08
Jahangir Shahzad9-Apr-09 20:08 
GeneralRe: wrong specifications of -vframes Pin
aaa20092-Nov-10 22:10
aaa20092-Nov-10 22:10 
GeneralQuestion : abt ffmpeg Pin
sohil vohra1-Apr-09 2:01
sohil vohra1-Apr-09 2:01 
GeneralGreate article, Thank you so much for a simple and clear code for how to use ffmpeg Pin
jefferycxl23-Feb-09 12:25
jefferycxl23-Feb-09 12:25 
Big Grin | :-D

I have been asked to add a video sharing feature to an asp.net website, I first went to http://www.sothink.com/[^]. It will cost $600 US.

Then I found ffmpeg does exactly the same thing for free, with your code, all I need to do it to add my swf player to let it play the flv file generated.

I am still doing research on whether there will be any issue of using ffmpeg in the commerical environment.

Any comments or suggestion would be welcome.

Thank you!

Jefferycxl

GeneralRe: Greate article, Thank you so much for a simple and clear code for how to use ffmpeg Pin
sohil vohra1-Apr-09 2:02
sohil vohra1-Apr-09 2:02 
GeneralMy vote of 1 Pin
twxx29-Nov-08 7:34
twxx29-Nov-08 7:34 
QuestionWhat if the ffmpeg.exe fails to convert a file ? Pin
Suresh_KSB11-Nov-08 9:06
Suresh_KSB11-Nov-08 9:06 
GeneralCant find ffmpeg Pin
parkerdc19-Aug-08 8:39
parkerdc19-Aug-08 8:39 
GeneralRe: Cant find ffmpeg Pin
Neutronet9-Sep-08 21:22
Neutronet9-Sep-08 21:22 
QuestionProblem with FLV Pin
Neutronet6-Aug-08 2:03
Neutronet6-Aug-08 2:03 
AnswerRe: Problem with FLV Pin
Jahangir Shahzad8-Apr-09 21:43
Jahangir Shahzad8-Apr-09 21:43 
AnswerOk i found solution Pin
Jahangir Shahzad8-Apr-09 21:51
Jahangir Shahzad8-Apr-09 21:51 
Generalthe cmd line doesn't work plz see that Pin
Eslam Egypt28-Jun-08 0:51
Eslam Egypt28-Jun-08 0:51 
GeneralRe: the cmd line doesn't work plz see that Pin
sohil vohra1-Apr-09 2:05
sohil vohra1-Apr-09 2:05 
GeneralYour approach is terrible, bad code Pin
Bill SerGio, The Infomercial King23-Jun-08 1:01
Bill SerGio, The Infomercial King23-Jun-08 1:01 
GeneralRe: Your approach is terrible, bad code Pin
sadegh hsn23-Jun-08 2:51
sadegh hsn23-Jun-08 2:51 
GeneralRe: Your approach is terrible, bad code Pin
aaa20091-Nov-10 22:22
aaa20091-Nov-10 22:22 
QuestionReturn or log of ffmpeg ? Pin
eulalie3-Feb-08 22:51
eulalie3-Feb-08 22:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.