Media processing on upload (audio compression)

Hi! I’m looking for some initial direction here!

Is there any way to trigger some behaviour whenever a file is uploaded to Media?

Specific use case - audio compression:

  1. Audio file (.WAV) is uploaded
  2. Process the file with a library like FFMpegCore to create a new file in compressed audio format (.MP3)
  3. New file added into Media library

Is this something that might be possible?

TIA!

Rich

Hey @jacksorjacksor! There certainly is. You’d be looking to hook into one of the Media notifications, like MediaSavingNotification or MediaSavedNotification:

A very simplified example where I was trying to get length metadata from a file in Blob storage…

public class MediaSavingNotificationHandler : INotificationHandler<MediaSavingNotification>
{
    private string[] fileTypes = new string[] { ".mp3" };
    private readonly IShortStringHelper _shortStringHelper;
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly IAzureBlobFileSystemProvider _azureBlobFileSystemProvider;

    public MediaSavingNotificationHandler(IShortStringHelper shortStringHelper, IAzureBlobFileSystemProvider fileSystemProvider, IHttpContextAccessor httpContextAccessor)
    {
        _shortStringHelper = shortStringHelper;
        _httpContextAccessor = httpContextAccessor;
        _azureBlobFileSystemProvider = fileSystemProvider;
    }

    public void Handle(MediaSavingNotification notification)
    {
        foreach (var publishedItem in notification.SavedEntities)
        {
            bool isFile = publishedItem.ContentType.Alias == Conventions.MediaTypes.File;
            bool isImage = publishedItem.ContentType.Alias == Conventions.MediaTypes.Image;
            bool isAudio = publishedItem.ContentType.Alias == Conventions.MediaTypes.Audio;
            bool isVector = publishedItem.ContentType.Alias == Conventions.MediaTypes.VectorGraphics;

            if (isFile || isAudio)
            {
                if (publishedItem.HasProperty("umbracoFile"))
                {
                    var httpContextRequest = _httpContextAccessor.HttpContext.Request;
                    string filePath = publishedItem.GetValue<string>("umbracoFile");
                    string absoluteFilePath = $"{httpContextRequest.Scheme}://{httpContextRequest.Host}{filePath}";

                    string fileExt = Path.GetExtension(filePath);
                    string fileName = Path.GetFileName(filePath);
                    bool isAudioFile = fileTypes.Contains(fileExt);

                    if (isAudioFile)
                    {
                        using (var httpClient = new HttpClient())
                        {
                            byte[] fileBytes = httpClient.GetByteArrayAsync(absoluteFilePath).Result;
                            var fileBytesAbstraction = new FileBytesAbstraction(fileName, fileBytes);

                            TagLib.File file = TagLib.File.Create(fileBytesAbstraction);

                            if (file != null)
                            {
                                TimeSpan ts = file.Properties.Duration;
                                string durationString = string.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds);

                                if (!publishedItem.HasProperty("umbracoLength"))
                                {
                                    publishedItem.Properties.Add(new Property(new PropertyType(_shortStringHelper, PropertyEditors.Aliases.Label, ValueStorageType.Ntext, "umbracoLength")));
                                }

                                publishedItem.SetValue("umbracoLength", durationString);
                            }
                        }
                    }
                }
            }
        }
    }
}

Hey @rickbutterfield - beautiful, thank you kindly!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.