zoukankan      html  css  js  c++  java
  • Converting , extracting preview image in asp.net via 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 X muxed audio and video
    MPEG-2 PS X X also known as VOB file
    MPEG-2 TS
    X also known as DVB Transport Stream
    ASF X X
    AVI X X
    WAV X X
    Macromedia Flash X X Only embedded audio is decoded.
    FLV X X Macromedia 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/Alaw X 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 X MPEG-4 is a variant of QuickTime.
    Raw MPEG4 video X X
    DV X X
    4xm
    X 4X Technologies format, used in some games.
    Playstation STR
    X
    Id RoQ X X Used in Quake III, Jedi Knight 2, other computer games.
    Interplay MVE
    X Format used in various Interplay computer games.
    WC3 Movie
    X Multimedia format used in Origin's Wing Commander III computer game.
    Sega FILM/CPK
    X Used in many Sega Saturn console games.
    Westwood Studios VQA/AUD
    X Multimedia formats used in Westwood Studios games.
    Id Cinematic (.cin)
    X Used in Quake II.
    FLIC format
    X .fli/.flc files
    Sierra VMD
    X Used in Sierra CD-ROM games.
    Sierra Online
    X .sol files used in Sierra Online games.
    Matroska X X
    Electronic Arts Multimedia
    X Used 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
    X Multimedia format used in games like Mad Dog McCree
    AVS
    X Multimedia format used by the Creature Shock game.
    Smacker
    X Multimedia format used by many games.
    GXF X X General eXchange Format SMPTE 360M, used by Thomson Grass Valley playout servers.
    CIN
    X Multimedia format used by Delphine Software games.
    MXF
    X Material eXchange Format SMPTE 377M, used by D-Cinema, broadcast industry.
    SEQ
    X Tiertex .seq files used in the DOS CDROM version of the game Flashback.
    DXA
    X This format is used in non-Windows version of Feeble Files game and different game cutscenes repacked for use with ScummVM.
    THP
    X Used on the Nintendo GameCube.
    C93
    X Used in the game Cyberia from Interplay.
    Bethsoft VID
    X Used in some games from Bethesda Softworks.
    CRYO APC
    X Audio 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 IX MP3 encoding is supported through the external library LAME.
    AC3 IX IX liba52 is used internally for decoding.
    Vorbis X X
    WMA V1/V2 X X
    AAC X X Supported 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
    X Used in some Sega Saturn console games.
    Duck DK4 IMA ADPCM
    X Used in some Sega Saturn console games.
    Westwood Studios IMA ADPCM
    X Used in Westwood Studios games like Command and Conquer.
    SMJPEG IMA ADPCM
    X Used in certain Loki game ports.
    CD-ROM XA ADPCM
    X
    CRI ADX ADPCM X X Used in Sega Dreamcast games.
    Electronic Arts ADPCM
    X Used in various EA titles.
    Creative ADPCM
    X 16 -> 4, 8 -> 4, 8 -> 3, 8 -> 2
    THP ADPCM
    X Used on the Nintendo GameCube.
    RA144
    X Real 14400 bit/s codec
    RA288
    X Real 28800 bit/s codec
    RADnet X IX Real low bitrate AC3 codec, liba52 is used for decoding.
    AMR-NB X X Supported through an external library.
    AMR-WB X X Supported through an external library.
    DV audio
    X
    Id RoQ DPCM X X Used in Quake III, Jedi Knight 2, other computer games.
    Interplay MVE DPCM
    X Used in various Interplay computer games.
    Xan DPCM
    X Used in Origin's Wing Commander IV AVI files.
    Sierra Online DPCM
    X Used 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
    X QuickTime fourcc 'alac'
    FFmpeg Sonic X X experimental lossy/lossless codec
    Qdesign QDM2
    X there are still some distortions
    Real COOK
    X All versions except 5.1 are supported
    DSP Group TrueSpeech
    X
    True Audio (TTA)
    X
    Smacker Audio
    X
    WavPack Audio
    X
    Cin Audio
    X Codec used in Delphine Software games.
    Intel Music Coder
    X
    Musepack
    X Only 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 )

    Collapse
             // 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 :

    Collapse
    //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)

    Download WebSite - 2,183 KB
  • 相关阅读:
    论文阅记 YOLOv4: Optimal Speed and Accuracy of Object Detection
    【项目实战】yolov3-tiny人脸数据模型训练
    面试题54. 二叉搜索树的第k大节点
    102. 二叉树的层序遍历
    107. 二叉树的层次遍历 II
    连续子数组的最大和
    172. 阶乘后的零
    26 进制
    405. 数字转换为十六进制数
    504. 七进制数
  • 原文地址:https://www.cnblogs.com/top5/p/1615770.html
Copyright © 2011-2022 走看看