Link to home
Start Free TrialLog in
Avatar of M.L. Martin
M.L. MartinFlag for United States of America

asked on

Convert other file types to .mp4 on upload (ASP.Net)

I want to make it very clear that what I am asking help with is definitely beyond my coding expertise. I will need lots of help to do this. I am building a website that allows members to upload short videos. I store the video in a directory and I store the reference to the video in SQL Server 2008R2. I use Flowplayer to stream the video and play when the user selects one of them. My major issue is I won't be able to determine what video format a user is uploading. And if I did I would not prevent them. What I desperately need to be able to do us convert any video the user uploads to MP4 or .mp4. I am using an ASP.Net FileUpload control to upload the video. The upload and playback works perfectly but again I want to convert the files on the upload. Example...User uploads a .3gp file. I want to convert the file to .mp4 and store it as mp4. My hope is that someone can provide code that can be added to what I am pasting here to make this work. Thank you all in advance for your help.

................................The code behind that handles the file uploading....................................
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Partial Class member1001glf
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'exercisename.Attributes.Add("onblur", "WaterMark(this, event);")
        'exercisename.Attributes.Add("onfocus", "WaterMark(this, event);")

        'bodypartmg.Attributes.Add("onblur", "bodypart(this, event);")
        'bodypartmg.Attributes.Add("onfocus", "bodypart(this, event);")
        Image78.ImageUrl = "Images/menu-icon-large2.png"
    End Sub

    Protected Sub Option_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Image78.ImageUrl = "Images/" & DirectCast(sender, LinkButton).Text & ".png"
    End Sub

    Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        ' Before attempting to save the file, verify
        ' that the FileUpload control contains a file.
        If (FileUpload1.HasFile) Then
            ' Call a helper method routine to save the file.
            SaveFile(FileUpload1.PostedFile)
        Else
            ' Notify the user that a file was not uploaded.
            UploadStatusLabel.Text = "You did not specify a file to upload."
        End If

    End Sub

    Sub SaveFile(ByVal file As HttpPostedFile)

        ' Specify the path to save the uploaded file to.
        Dim savePath As String = HttpContext.Current.Server.MapPath("memuploads\")

        ' Get the name of the file to upload.
        Dim fileName As String = FileUpload1.FileName

        ' Create the path and file name to check for duplicates.
        Dim pathToCheck As String = savePath + fileName

        ' Create a temporary file name to use for checking duplicates.
        Dim tempfileName As String

        ' Check to see if a file already exists with the
        ' same name as the file to upload.        
        If (System.IO.File.Exists(pathToCheck)) Then
            Dim counter As Integer = 2
            While (System.IO.File.Exists(pathToCheck))
                ' If a file with this name already exists,
                ' prefix the filename with a number.
                tempfileName = counter.ToString() + fileName
                pathToCheck = savePath + tempfileName
                counter = counter + 1
            End While

            fileName = tempfileName

            ' Notify the user that the file name was changed.
            UploadStatusLabel.Text = "A file with the same name already exists." + "<br />" + _
                                     "Your file was saved as " + fileName

        Else

            ' Notify the user that the file was saved successfully.
            UploadStatusLabel.Text = "Your file was uploaded successfully."

        End If

        ' Append the name of the file to upload to the path.
        savePath += fileName

        ' Call the SaveAs method to save the uploaded
        ' file to the specified directory.
        FileUpload1.SaveAs(savePath)

        Dim MemUser As MembershipUser
        MemUser = Membership.GetUser()
        UserId.Value = MemUser.ProviderUserKey.ToString()

        Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("ConnStringUpload").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim strQuery As String = "insert into tblFiles (UserId, savedfor, searchcode, exercisename, bodypartmg, bodypartspecific, description, FileName, FilePath) values(@UserId, @savedfor, @searchcode, @exercisename, @bodypartmg, @bodypartspecific, @description, @FileName, @FilePath)"
        Dim cmd As New SqlCommand(strQuery)
        cmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = MemUser.ProviderUserKey.ToString()
        cmd.Parameters.Add("@SavedFor", SqlDbType.VarChar).Value = savedfor.Text
        cmd.Parameters.Add("@searchcode", SqlDbType.VarChar).Value = searchcode.Text
        cmd.Parameters.Add("@exercisename", SqlDbType.VarChar).Value = exercisename.Text
        cmd.Parameters.Add("@bodypartmg", SqlDbType.VarChar).Value = bodypartmg.Text
        cmd.Parameters.Add("@bodypartspecific", SqlDbType.VarChar).Value = bodypartspecific.Text
        cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = description.Text
        cmd.Parameters.AddWithValue("@FileName", fileName)
        cmd.Parameters.AddWithValue("@FilePath", "memuploads\" & fileName)
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Try
            con.Open()
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            Response.Write(ex.Message)
        Finally
            con.Close()
            con.Dispose()
        End Try

    End Sub
End Class

------------------------The aspx (User Interface) for uploading the file-------------------------------------
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="member1001glf.aspx.vb" Inherits="member1001glf" %>
  <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <%--<meta charset="utf-8" />--%>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
     <script type="text/javascript" src="jwplayer.js"></script>
    <title>Upload Files To DataBase</title>
     <link href="MobileCss.css" rel="stylesheet" type="text/css" />
    <link href="trial_menu_files/css3menu1/style.css" rel="stylesheet" />
    <link href="final_menu/style.css" rel="stylesheet" />

    <%--Exercise Name Java Script Here--%>
    <script language="javascript" type="text/javascript">
        function en(txtName, event) {
            var defaultText = "Enter Exercise Name*";
            // Condition to check textbox length and event type
            if (txtName.value.length == 0 & event.type == "blur") {
                //if condition true then setting text color and default text in textbox
                txtName.style.color = "Gray";
                txtName.value = defaultText;
            }
            // Condition to check textbox value and event type
            if (txtName.value == defaultText & event.type == "focus") {
                txtName.style.color = "black";
                txtName.value = "";
            }
        }
</script>
    <%--End JavaScript--%>
    <%--Major Bodypart Java Script Here--%>
    <script language="javascript" type="text/javascript">
        function bp(txtName, event) {
            var defaultText = "Enter Bodypart*";
            // Condition to check textbox length and event type
            if (txtName.value.length == 0 & event.type == "blur") {
                //if condition true then setting text color and default text in textbox
                txtName.style.color = "Gray";
                txtName.value = defaultText;
            }
            // Condition to check textbox value and event type
            if (txtName.value == defaultText & event.type == "focus") {
                txtName.style.color = "black";
                txtName.value = "";
            }
        }
</script>
    <%--End JavaScript--%>
    <%--Major Bodypart Specific Java Script Here--%>
    <script language="javascript" type="text/javascript">
        function bps(txtName, event) {
            var defaultText = "Target Bodypart";
            // Condition to check textbox length and event type
            if (txtName.value.length == 0 & event.type == "blur") {
                //if condition true then setting text color and default text in textbox
                txtName.style.color = "Gray";
                txtName.value = defaultText;
            }
            // Condition to check textbox value and event type
            if (txtName.value == defaultText & event.type == "focus") {
                txtName.style.color = "black";
                txtName.value = "";
            }
        }
</script>
    <%--End JavaScript--%>
    <%--Trainee's Name Java Script Here--%>
    <script language="javascript" type="text/javascript">
        function trainee(txtName, event) {
            var defaultText = "Trainee's Name";
            // Condition to check textbox length and event type
            if (txtName.value.length == 0 & event.type == "blur") {
                //if condition true then setting text color and default text in textbox
                txtName.style.color = "Gray";
                txtName.value = defaultText;
            }
            // Condition to check textbox value and event type
            if (txtName.value == defaultText & event.type == "focus") {
                txtName.style.color = "black";
                txtName.value = "";
            }
        }
</script>
    <%--End JavaScript--%>
    <%--Trainee's Name Java Script Here--%>
    <script language="javascript" type="text/javascript">
        function code(txtName, event) {
            var defaultText = "Enter Keyword";
            // Condition to check textbox length and event type
            if (txtName.value.length == 0 & event.type == "blur") {
                //if condition true then setting text color and default text in textbox
                txtName.style.color = "Gray";
                txtName.value = defaultText;
            }
            // Condition to check textbox value and event type
            if (txtName.value == defaultText & event.type == "focus") {
                txtName.style.color = "black";
                txtName.value = "";
            }
        }
</script>
    <%--End JavaScript--%>
    <%--Trainee's Name Java Script Here--%>
    <script language="javascript" type="text/javascript">
        function desc(txtName, event) {
            var defaultText = "Brief Description";
            // Condition to check textbox length and event type
            if (txtName.value.length == 0 & event.type == "blur") {
                //if condition true then setting text color and default text in textbox
                txtName.style.color = "Gray";
                txtName.value = defaultText;
            }
            // Condition to check textbox value and event type
            if (txtName.value == defaultText & event.type == "focus") {
                txtName.style.color = "black";
                txtName.value = "";
            }
        }
</script>
    <%--End JavaScript--%>

</head>

    <style type="text/css">
        .smallfont {
            font-size: 8pt;
            font-family:arial, Verdana;
        }
       
        .watermark
        {
         width:150px;
         font:Verdana;
         font-style:italic;
         color:GrayText;
        }

    </style>

<body class="grad">
    <form id="form1" runat="server">
        <div id="pageTop" class="TopNav"><table style="margin-top: -6px"><tr><td id="5" valign="center"><asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
        <asp:DropDownExtender ID="DropDownExtender1" runat="server"
                 DropDownControlID="Panel1" TargetControlID="Image78"></asp:DropDownExtender>
                <asp:Image ID="Image78" runat="server" BorderStyle="None" ImageUrl="~/images/menu-icon-large2.png">
                </asp:Image>
                                       
                     
                   
            <asp:Panel ID="Panel1" runat="server"  Width="100%" BackColor="#428BCA"
                        HorizontalAlign="Left" BorderWidth="1px" BorderColor="White"
                        Font-Names="Arial" Font-Size="9pt"  Font-Bold="False" ForeColor="#00FFFF">
                 
                <table style="font-family: Arial, Helvetica, sans-serif; font-size: 9pt; font-weight: normal; color: #FFFFFF;">
                <tr><td style="color: #FFFFFF">
                    <asp:LinkButton ID="LinkButton8" runat="server" PostBackUrl="~/register.aspx"
                     CssClass="linkbutton">Free Membership</asp:LinkButton></td></tr>
                <tr><td><asp:LinkButton ID="LinkButton11" runat="server" PostBackUrl="~/support.aspx"
                     CssClass="linkbutton">Member Sharing PIN</asp:LinkButton></td></tr>
                <tr><td class="style129"><asp:LinkButton ID="LinkButton10" runat="server" PostBackUrl="~/Default.aspx"
                     CssClass="linkbutton">GoLiveFitness Home</asp:LinkButton></td></tr>
                <tr><td class="style129">
                <asp:LinkButton ID="option3" runat="server" PostBackUrl="~/m_search.aspx"
                     >Search Exercises</asp:LinkButton><br />
                    </td>
                    </tr>
                    <tr><td class="style129"><asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/Support.aspx"
                    >Support</asp:LinkButton></td></tr>
                   
                    <tr><td class="style129"><asp:LinkButton ID="LinkButton2" runat="server" PostBackUrl="~/thefitcafe.aspx"
                    >The Fit Cafe</asp:LinkButton></td></tr>
                   
                    <tr><td class="style129"><asp:LinkButton ID="LinkButton3" runat="server" PostBackUrl="~/modelgallery.aspx"
                    >Model Photo Gallery</asp:LinkButton></td></tr>
                   
                    <tr><td class="style129"><asp:LinkButton ID="LinkButton4" runat="server" PostBackUrl="~/sponsors.aspx"
                    ForeColor="#49649B" >Sponsorships</asp:LinkButton></td></tr>
                   
                    <tr><td class="style129"><asp:LinkButton ID="LinkButton5" runat="server" PostBackUrl="~/traincoach.aspx"
                    ForeColor="#49649B" >Trainers, Fitness Coaches</asp:LinkButton></td></tr>
                    <tr><td class="style129"><asp:LinkButton ID="LinkButton6" runat="server" PostBackUrl="~/exermodels.aspx"
                    ForeColor="#49649B" >Become an Exercise Performer</asp:LinkButton></td></tr>
                    <tr><td class="style129"><asp:LinkButton ID="LinkButton7" runat="server" PostBackUrl="~/events.aspx"
                    ForeColor="#49649B" >Scheduled Events</asp:LinkButton></td></tr>
                    <tr><td class="style129"><asp:LinkButton ID="LinkButton9" runat="server" PostBackUrl="~/searchbymodel.aspx"
                    ForeColor="#49649B" >Exercises by Models</asp:LinkButton></td></tr>
                    <tr><td>zzzzzzzzzz</td></tr>
                    </table>
                   
            </asp:Panel></td><td id="10" valign="center"><table>
   <tr><td width="100%" height="100%" align="left" valign="top" border width="1">
      </td>
       <td width="100%" height="100%" align="left" valign="center" border width="1">
        <asp:TextBox ID="search" runat="server" Text = "Enter Bodypart"  ForeColor = "Gray" onblur = "WaterMark(this, event);" onfocus = "WaterMark(this, event);" Height="22px"></asp:TextBox>
       </td><td>
           <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/searchPng2.png" PostBackUrl="~/m_newdirect.aspx" /></td>
   </table></td></tr></table>
    </div>
       <div id="infoHeader" style="font-family: Arial, Helvetica, sans-serif; font-size: 9pt; font-style: normal; font-variant: small-caps; text-transform: capitalize; color: #428BCA; font-weight: bolder; padding-top: 5px;">Mobile GoLiveFitness: Search<br /> 1000's of exercises and upload<br /> your own!</div>
  <div>
     
            <asp:LoginName ID="LoginName1" runat="server" Font-Names="Arial" Font-Size="8pt" />
           
      <p class="smallfont">Members upload your <br />exercise video</p>
       <asp:FileUpload type="file" accept="video/.mp4" ID="FileUpload1" runat="server" Width="180px" /><br />
        <asp:RegularExpressionValidator ID="uplValidator" runat="server" ControlToValidate="FileUpload1"
 ErrorMessage="Only .mp4 video formats are supported"
 ValidationExpression="^([0-9a-zA-Z_\-~ :\\])+(.3gp|.3GP|.mov|.MOV|.wav|.WAV|.wmv|.WMV|.M4V|.m4v|.mp4|.MP4)$" Font-Names="Arial" Font-Size="8pt" ForeColor="Red"></asp:RegularExpressionValidator><br />
        <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
        <br />
        <asp:Label ID="lblMessage" runat="server" Font-Names = "Arial" Font-Size="8pt" ForeColor="#ABABAB"></asp:Label>
    </div>
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SQL2008R2_504887_golivefitnesConnectionString %>" SelectCommand="SELECT [id], [Name], [ContentType] FROM [tblFiles]"></asp:SqlDataSource>
        </div>
        <div></div>
        <div>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:SQL2008R2_504887_golivefitnesConnectionString %>" SelectCommand="SELECT [Data] FROM [tblFiles] WHERE ([id] = 6)">
                <SelectParameters>
                    <asp:Parameter DefaultValue="6" Name="id" Type="Int32" />
                </SelectParameters>
            </asp:SqlDataSource>
        </div>
        <div>
            <asp:Label ID="UploadStatusLabel" runat="server"></asp:Label>
        </div>
        <div>
            <asp:TextBox ID="exercisename" runat="server" Text = "Enter Exercise Name*"  ForeColor = "Gray" onblur = "en(this, event);" onfocus = "en(this, event);" BackColor="#D7EBFF"></asp:TextBox>
        </div>
        <div>
            <asp:TextBox ID="bodypartmg" runat="server" Text = "Enter Bodypart*"  ForeColor = "Gray" onblur = "bp(this, event);" onfocus = "bp(this, event);" BackColor="#D7EBFF"></asp:TextBox></div>
       
        <div>
            <asp:TextBox ID="bodypartspecific" runat="server" Text = "Target Bodypart"  ForeColor = "Gray" onblur = "bps(this, event);" onfocus = "bps(this, event);" BackColor="#D7EBFF"></asp:TextBox></div>
       
            <div>
            <asp:TextBox ID="savedfor" runat="server" Text = "Trainee's Name"  ForeColor = "Gray" onblur = "trainee(this, event);" onfocus = "trainee(this, event);" BackColor="#D7EBFF"></asp:TextBox></div>
        <div>
            <div>
            <asp:TextBox ID="searchcode" runat="server" Text = "Enter Keyword"  ForeColor = "Gray" onblur = "code(this, event);" onfocus = "code(this, event);" BackColor="#D7EBFF"></asp:TextBox></div>
            <asp:TextBox ID="description" runat="server" Height="60px" Rows="3" Text = "Brief Description"  ForeColor = "Gray" onblur = "desc(this, event);" onfocus = "desc(this, event);" BackColor="#D7EBFF"></asp:TextBox></div>
        <div>
            <asp:HiddenField ID="UserId" runat="server" />
        </div>
    </form>
    <p>
        &nbsp;</p>
</body>
</html>
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

I would suggest you find a video conversion tool which supports command line parameters. You can then call this converter from your VB.NET using Process.Start (http://www.dotnetperls.com/process-vbnet). This might be the easiest route. I am not aware of a .NET library that converts between numerous video formats.

MoveAvi
http://www.movavi.com/video-converter-command-line/how-it-works.html

Format Factory
A command line along these lines (I got this from http://www.wxforum.net/index.php?topic=18011.0)

"C:\Program Files (x86)\FreeTime\FormatFactory\formatfactory" "All to WMV" "High quality and size" "k:\mymovie.avi" %1

"All to WMV" and "High quality and size" are actually parameters passed to formatfactory. "k:\mymovie.avi" is the avi created by MovieSalsa. "%1" points to the output wmv file.
ASKER CERTIFIED SOLUTION
Avatar of Ron Malmstead
Ron Malmstead
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of M.L. Martin

ASKER

Both of these look like they will get the job done. I have read good reviews on the products so I will give them a try.