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

FFMPEG using ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.45/5 (14 votes)
8 Jul 2008CPOL2 min read 161.2K   50   44
This piece of code will show you to convert any video file format to high quality flv and stream it on web server

Introduction

Many of us have seen that, package like FFMPEG is quite hectic to use in our web application. Many of us have already opt different modules like Media Handler etc. to convert their video files to flv and stream it over the server.

This module will help you to convert your video files to flv which can easily stream on the server. Moreover you can can get high quality flv using this code.

Background

The very basic idea behind this article is to use the FFMPEG command line argument to fulfill the requirement. All other popular modules available in the internet are based on those command lines which you can use directly to achieve your goal.

Using the code

Here is the website which I tried to develop. Its still under construction. So some bugs you may find over there. But the conversion portion is working fine.

[Sorry I have to close the link as some spamming is done in my server to this application. If anyone is interested then please contact me in my EMail id, I will help him/her as far as I can]

here are the steps...

Step - 1

Just download FFMPEG module from the internet. The latest version of FFMPEG will be a zip file which contains all the source code, FFMPEG.exe, FLVTOOL.exe and more. Extract those files into your application directory. FFMPEG.exe basically convert the video files into flv and FLVTOOL insert the metadata to the flv files. So that the video files will have all the video information.Without the metadata the video files won't be able to stream properly.

Step -2

Assign the exepath and directories.

AppPath = Request.PhysicalApplicationPath 'Get the application path
inputPath = AppPath & "videos\org_video" 'Path of the original file
outputPath = AppPath & "videos\conv_video" 'Path of the converted file
imgpath = AppPath & "videos\thumbs" 'Path of the generated thumanails
prevpath = AppPath & "videos\preview" 'Path of the preview file

Now the main thing is the command line that executes ffmpeg.exe on the server...

string cmd as string = " -i """ & newPath & """ """ & outputPath & "\" & out & 
"""" '-i specifies the input file

Here newpath is the path of the source video file, outputpath is path of the converted video and out is the filename.

The above command line will convert the video into flv format. Now if you like to get high quality flv then use the following code..

Dim fileargs As String = "-i " & newPath & " -acodec mp3 -ab 64k -ac 2 
-ar 44100 -f flv -deinterlace -nr 500 -croptop 4 -cropbottom 8 -cropleft 
8 -cropright 8 -s 640x480 -aspect 4:3 -r 25 -b 650k -me_range 25 -i_qfactor 
0.71 -g 500 " & outputPath & "\" & out & ""
The above code will generate high quality flv. Now to insert metadata into those generated flv file use the following command line
Dim filebuffer As String = "-U """ & outputPath & "\" & out & """"
To create thumbnail use the following code....
Dim imgargs As String = " -i """ & newPath & """ -f image2 -ss 1 -vframes 1 -s 
80x60 -an """ & imgpath & "\" & thumbnm & """"
-i = input file -f = file format -vframe = video frame -s = size

Now create a function which can execute those command line

       Dim proc As New System.Diagnostics.Process()
        proc.StartInfo.FileName = exepath 
'Path of exe that will be executed, only for "filebuffer" it will be "flvtool2.exe"
        proc.StartInfo.Arguments = cmd 'The command which will be executed
        proc.StartInfo.UseShellExecute = False
        proc.StartInfo.CreateNoWindow = True
        proc.StartInfo.RedirectStandardOutput = False
        proc.Start()
Step - 3

The above code will convert any kind of video into FLV now the only thing you have to do is to stream the video using any flash video player.

Points of Interest

You can easily develop any streaming video application using this code and easily build a site like youtube or sharkle. For any kind of help feel free to mail me at arinhere@gmail.com

License

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


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSource code written in C# Pin
naveen Tiwari Ji8-Sep-15 2:23
professionalnaveen Tiwari Ji8-Sep-15 2:23 
GeneralMy vote of 1 Pin
ramana40719-Feb-14 20:04
ramana40719-Feb-14 20:04 
GeneralMy vote of 2 Pin
ramana40719-Feb-14 20:01
ramana40719-Feb-14 20:01 
QuestionCan't get it to work with file upload Pin
PorkyGimp21-Feb-12 20:42
PorkyGimp21-Feb-12 20:42 
Hi
I am try to get your code to work with a file upload control. It goes through the code and seem to be doing something, but i'm not getting any output? I read on the asp forum that you commented on about a sdl.dll. I'm not sure about that either.
I have listed my code below.

Please help me.

VB
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConfirm.Click

       If IsPostBack Then
           'declare the file path to store uploads
           Dim path As String = Server.MapPath("~/UploadedVideos/")
           Dim fileOK As Boolean = False

           'If the file upload control has a file
           If FileUploadVideo.HasFile Then
               'check to see if the number of characters does not exceed 50 and is not empty
               If Len(txtVideoName.Text) <= 50 And txtVideoName.Text IsNot "" Then
                   'clear the error message
                   lblErrorTitle.Text = ""
                   'check to see if a course has been added to the list
                   If lstCourses.SelectedIndex <> -1 Then
                       lblErrorCourse.Text = ""
                       'check to see if a module has been added to the list
                       If lstModules.SelectedIndex <> -1 Then
                           lblErrorModule.Text = ""

                           'check to see if the number of characters does not exceed 50 and is not empty
                           If Len(txtKeywords.Text) <= 50 And txtKeywords.Text IsNot "" And txtKeywords.Text = Replace(txtKeywords.Text, " ", "") Then

                               'clear the error message
                               lblErrorKeywords.Text = ""

                               'check the file to make sure it is of the required type
                               Dim fileExtension As String
                               fileExtension = System.IO.Path. _
                                   GetExtension(FileUploadVideo.FileName).ToLower()
                               'declare the allowed extentions
                               Dim allowedExtensions As String() = _
                                   {".mov", ".wmv", ".avi", ".vob", ".mp4"}
                               'loop to check the extention type
                               For i As Integer = 0 To allowedExtensions.Length - 1
                                   If fileExtension = allowedExtensions(i) Then

                                       '//////////////////////////// Covert File Here

                                       Dim AppPath As String = Request.PhysicalApplicationPath & "UploadedVideos\"
                                       Dim inputPath As String = FileUploadVideo.FileName                       'Source Video Path
                                       Dim outputPath As String = AppPath & "myVideoOutput.flv"      'Destination Video Path
                                       Dim fileargs As String = " -i """ & inputPath & """ """ & outputPath & """"              'Command Line
                                       Dim proc As New Diagnostics.Process()
                                       Dim filebuffer As String = "-U """ & outputPath & "\" & """"
                                       proc.StartInfo.FileName = AppPath & "ffmpeg.exe"                                   'Path of FFMPEG
                                       proc.StartInfo.Arguments = fileargs
                                       proc.StartInfo.UseShellExecute = False
                                       proc.StartInfo.CreateNoWindow = False
                                       proc.StartInfo.RedirectStandardOutput = False
                                       proc.Start()
                                       fileOK = True
                                   End If
                                   '////////////////////////////////Upload File path
                               Next
                               If fileOK Then
                                   Using Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
                                       Try
                                           'open the database connection
                                           Conn.Open()
                                           Dim FilePath = System.AppDomain.CurrentDomain.BaseDirectory & "UploadedVideos\" & FileUploadVideo.FileName
                                           'Dim FilePath = path & FileUploadVideo.FileName
                                           'insert statement to add the video details to the database
                                           Const SQL As String = "INSERT INTO [Video] ([VideoName], [CourseNo], [ModuleNo], [DateUploaded], [VideoUrl], [Keywords]) VALUES (@VideoName, @CourseNo, @ModuleNo, @DateUploaded, @VideoUrl, @Keywords)"
                                           Dim cmd As New SqlCommand(SQL, Conn)
                                           cmd.Parameters.AddWithValue("@VideoName", txtVideoName.Text.Trim())
                                           cmd.Parameters.AddWithValue("@CourseNo", cboCourse.SelectedValue())
                                           cmd.Parameters.AddWithValue("@ModuleNo", cboModule.SelectedValue())
                                           cmd.Parameters.AddWithValue("@DateUploaded", Date.Now)
                                           cmd.Parameters.AddWithValue("@VideoUrl", FilePath)
                                           cmd.Parameters.AddWithValue("@Keywords", txtKeywords.Text.Trim())
                                           FileUploadVideo.PostedFile.SaveAs(path & _
                                                FileUploadVideo.FileName)

                                           'MsgBox("File Uploaded Successfully - Thank You", MsgBoxStyle.Information, "Video Upload")
                                           'Successful upload message
                                           lblError.Text = "File Uploaded Successfully - Thank You"

                                           'clear the fields
                                           lblHoldFilePath.Text = ""
                                           lstModules.Items.Clear()
                                           lstCourses.Items.Clear()
                                           txtVideoName.Text = ""
                                           txtKeywords.Text = ""
                                           cboCourse.Text = ""
                                           cboModule.Text = ""
                                           cmd.ExecuteNonQuery()

                                           'close the database connection
                                           Conn.Close()
                                       Catch ex As Exception

                                           'file can't be uploaded error message
                                           lblError.Text = "File could not be uploaded." & ex.Message
                                       End Try
                                   End Using
                               Else
                                   'error message for incorrect file type
                                   lblError.Text = "Cannot accept files of this type."
                               End If


                           Else
                               'error message for too many characters in the video name field
                               lblErrorKeywords.Text = "This is a required field with a maximum length of 50 characters and no spaces!"
                           End If

                       Else
                           lblErrorModule.Text = "You must add at least 1 Module to the list"
                       End If
                   Else
                       lblErrorCourse.Text = "You must add at least 1 course to the list"
                   End If
                   Else
                       'error message for too many characters in the keywords field
                       lblErrorTitle.Text = "This is a required field with a maximum length of 50 characters"
                   End If
               Else
                   'error message for no file selected
               lblHoldFilePath.Text = "Please Choose A File!!"
               End If
           End If


   End Sub

AnswerRe: Can't get it to work with file upload Pin
arinhere30-Jan-13 20:23
arinhere30-Jan-13 20:23 
QuestionWill this work for concurrency users Pin
Manjunath T - Mysore17-Jan-12 22:22
Manjunath T - Mysore17-Jan-12 22:22 
AnswerRe: Will this work for concurrency users Pin
arinhere30-Jan-13 20:20
arinhere30-Jan-13 20:20 
QuestionFFMPEG using ASP.NET with C# Pin
divydivy4-Dec-11 20:56
divydivy4-Dec-11 20:56 
AnswerRe: FFMPEG using ASP.NET with C# Pin
arinhere4-Dec-11 21:07
arinhere4-Dec-11 21:07 
GeneralMy vote of 5 Pin
divydivy4-Dec-11 20:48
divydivy4-Dec-11 20:48 
QuestionASPX source for Grab thumbnail Pin
Nabbie21-Jun-11 23:10
Nabbie21-Jun-11 23:10 
AnswerRe: ASPX source for Grab thumbnail Pin
arinhere4-Dec-11 21:09
arinhere4-Dec-11 21:09 
GeneralSource Code Plz Pin
Chetan Chaudhari19-Apr-11 2:54
Chetan Chaudhari19-Apr-11 2:54 
Generalsource code Pin
yusufseyrek3-Jan-11 12:25
yusufseyrek3-Jan-11 12:25 
GeneralSource please Pin
zerataul8-Sep-10 22:52
zerataul8-Sep-10 22:52 
Generalc# code Pin
niravsahayata9-Jun-10 21:47
niravsahayata9-Jun-10 21:47 
GeneralPlease send me Code Pin
Revanth Arji4-Apr-10 19:46
Revanth Arji4-Apr-10 19:46 
GeneralI want a take a SNAP of video file at a time of Update video file. Pin
humill_MCA19-Feb-10 18:38
humill_MCA19-Feb-10 18:38 
GeneralRe: I want a take a SNAP of video file at a time of Update video file. Pin
arinhere30-Jan-13 20:25
arinhere30-Jan-13 20:25 
GeneralCreate Image from flash file Pin
By using our site you acknowledge4-Aug-09 20:44
By using our site you acknowledge4-Aug-09 20:44 
GeneralRe: Create Image from flash file Pin
arinhere4-Aug-09 21:05
arinhere4-Aug-09 21:05 
GeneralWorking project with realtime video conversion and Image generation Pin
Member 452158613-May-09 2:48
Member 452158613-May-09 2:48 
GeneralRe: Working project with realtime video conversion and Image generation Pin
Member 308579010-Aug-09 23:44
Member 308579010-Aug-09 23:44 
QuestionC# Code Pin
ramyamr14-Dec-08 21:50
ramyamr14-Dec-08 21:50 
AnswerRe: C# Code Pin
arinhere29-Dec-08 1:39
arinhere29-Dec-08 1:39 

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.