+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Lenght of mp3 file

Lenght of mp3 file

12 posts, 0 answered
  1. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    30 Aug 2010
    Link to this post
    Hi all,

    I have a question about getting data from a .mp3 file.
    I'd like to read the length of a .mp3 file and store it in a metafield of a library item.

    I see the download list control reads the size of a file, but I want to know how I can get the length in minutes / seconds of a .mp3.

    Anyone with an idea?

    Thanks,
    Daniel
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    30 Aug 2010
    Link to this post
    Hello Daniel,

    You can take a look at

    C# – How to read MP3 header?
    get the duration of an audio/mp3 file by using C#?

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    30 Aug 2010
    Link to this post
    Thanks Ivan.
    Could you tell me how I can read and save this information while uploading a file, or saving a file into the Library?

    Regards,
    Daniel
  4. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    30 Aug 2010
    Link to this post
    Hmmm... it isn't as easy as I thought.

    I'm using the option to store the files on the filesystem.
    I'm having trouble to execute this code:

    TagLib.File file = TagLib.File.Create(MapPath(item.UrlWithExtension);

    It say's it doesn't support the file extension.
    How can I refer to the .mp3 file?

    Thanks,
    Daniel
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    30 Aug 2010
    Link to this post
    Hi Daniel,

    To do this during upload, you need a custom control, because you will not be  able the built-in functionality that comes with LibraryItemUpload view. You need to implement a custom RadUpload control and use the API  to create a new content item

    Here is a sample code

    var manager = new LibraryManager("Libraries");
    var libraryManager = new ContentManager("Libraries");
    var imagesLibrary = manager.GetLibrary("Images");
    var allImages = imagesLibrary.GetItems();
    foreach (IContent image in allImages)
    {
        var temp = libraryManager.GetContent(image.ID);
        var newImage = libraryManager.CreateContent(temp.MimeType);
        newImage.ParentID = imagesLibrary.ID;
        libraryManager.SaveContent(newImage);
        newImage = (IStreamableContent)libraryManager.GetContent(newImage.ID);
        if (libraryManager.StreamingProvider != null && newImage != null)
        {
            using (var contentStream = libraryManager.StreamingProvider.GetUploadStream((IStreamableContent)newImage))
            {
                StreamHelper.CopyStream(new MemoryStream((byte[])image.Content), contentStream, false, libraryManager.Provider);
            }
            manager.SaveContent(newImage, ContentStatus.Published);
            var stagedImage = manager.GetCurrentState(newImage.ID,true);
            //copy metafields
            foreach (var metaKey in libraryManager.Provider.MetaKeys.Keys)
            {
                if (image.GetMetaData(metaKey.ToString()) != null)
                    stagedImage.SetMetaData(metaKey.ToString(), image.GetMetaData(metaKey.ToString()));
            }
            if (libraryManager.Provider.AllowComments)
                foreach (IComment comment in image.Comments)
                {
                    IComment newComment = libraryManager.CreateComment(stagedImage);
                    newComment.Author = comment.Author;
                    newComment.Email = comment.Email;
                    newComment.IpAddress = comment.IpAddress;
                    newComment.Owner = newComment.Owner;
                    newComment.Visible = comment.Visible;
                    newComment.Text = comment.Text;
                    newComment.WebSite = comment.WebSite;
                    libraryManager.SaveComment(newComment);
                }
            //copy item info
            if (image.ItemInfo != null)
                stagedImage.ItemInfo = image.ItemInfo;
            //copy thumbnails
            if (image.Thumbnails.Count > 0)
                foreach (var thumbnail in image.Thumbnails)
                {
                    try
                    {
                        stagedImage.Thumbnails.Add(thumbnail);
                    }
                    catch { }
                }
            libraryManager.DeleteContent(image);
            libraryManager.SaveContent(stagedImage,ContentStatus.Published);
        }
    }

    The same applies when you edit content item.

    The UrlWithExtension property of IContent object returns the extension that is passed by ContentHttpHandler - sflb.ashx.  You can use real file extensions  or try using the mime type.

    The Files Provider of Sitefinity does store the entire content in the file system ( but not the metadata) and we store only a file which type is "File", not a file with the actual extension ( mp3, mp4, mpeg etc)

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    30 Aug 2010
    Link to this post
    Thanks.

    I use this code:

    TagLib.File file = TagLib.File.Create(MapPath(item.Url + ".mp3"), item.MimeType, TagLib.ReadStyle.None);

    But then I get an error:

    Could not find a part of the path 'D:\Projects\Egz\Egz.Web.Cms\Media\Presentations\file_001.mp3'.

    Do I have to enable something to make direct access to this file available?

    Regards,
    Daniel

  7. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    07 Sep 2010
    Link to this post
    Anyone who knows how to directly access a mp3 file that is inside a library (content stored on the filesystem)
    This code doesn't work:

    string item = MapPath(item.Url + ".mp3")

    Regards,
    Daniel
  8. George
    George  avatar
    204 posts
    Registered:
    05 Sep 2006
    07 Sep 2010
    Link to this post
    Daniel,

    I think Sitefinity will save the file according to its name. So let's say it's stored in a folder called mp3 you can Map the path for the mp3 folder / the item.getMetaData("Name").ToString() + ".mp3"

    You can try this it should work ...
  9. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    09 Sep 2010
    Link to this post
    Thanks George, for your reply.

    If I use name I get the name of the media item, so for example:
    item.GetMetaData("Name").ToString()

    returns:
    'How to use scrum'

    If I use my own approach:
    item.Url + ".mp3"

    it returns:
    'How_to_use_scrum.mp3'

    If I fill this in inside my browser, the file cannot be found.
    So, the full URL then is: http://localhost/mywebsite/Media/Scrum/How_to_use_scrum.mp3

    If I use this approach:
    item.UrlWithExtension()

    it returns: http://localhost/mywebsite/Media/Scrum/How_to_use_scrum.sflb.ashx
    This works, but the TagLib library only works with real files.

    So, I'm a bit stuck here.

    Regards,
    Daniel

  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    09 Sep 2010
    Link to this post
    Hello Daniel,

    You can use real expressions with files form Images and Documents module


    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  11. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    09 Sep 2010
    Link to this post
    Hi Ivan,

    Okay, I have setup the custom handler for .mp3 files.
    So if I enter the direct link inside the browser's addressbar, like:

    http://localhost/mywebsite/Media/Scrum/How_to_use_scrum.mp3

    then it works. It downloads the .mp3.

    But now I'd like to refer to the physical file, so that I can use the TagLib library.
    A direct link doesn't seem to work. Also if I try it with the normal System.IO.File it says that the file doesn't exists?
    I use:

    MapPath(item.url + ".mp3")

    I know that SF stores only the content of the file on the harddrive, but the metadata is inside the database. Is there another approach to read the file lengt of the content?

    Regards,
    Daniel
  12. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    09 Sep 2010
    Link to this post
    Hello Daniel,

    In C# Server.MapPath specifies the relative or virtual path to map to a physical directory. Files uploaded to Images and Documents module are stored in the database and the link is dynamically constructed. These files are not stored on the file system and you cannot access them with Server.MapPath, because this method will search inside your file storage (like C:\Inetpub\wwwroot\). You can use Page.ResolveUrl or get the content item and save it in a temp folder. You can create byte array (byte[])item.Content; from the item content and then memory stream that you will save. By doing this the file will be saved to the file system( say in App_Data) and you can remove it when you do not need it.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
12 posts, 0 answered