Search code examples
pythonazureazure-media-services

Uploading a Video to Azure Media Services with Python SDKs


I am currently looking for a way to upload a video to Azure Media Services (AMS v3) via Python SDKs. I have followed its instruction, and am able to connect to AMS successfully.

Example

credentials = AdalAuthentication(
    context.acquire_token_with_client_credentials,
    RESOURCE,
    CLIENT,
    KEY)

client = AzureMediaServices(credentials, SUBSCRIPTION_ID) # Successful

I also successfully get all the videos' details uploaded via its portal

for data in client.assets.list(RESOUCE_GROUP_NAME, ACCOUNT_NAME).get(0):
    print(f'Asset_name: {data.name}, file_name: {data.description}')

# Asset_name: 4f904060-d15c-4880-8c5a-xxxxxxxx, file_name: 夢想全紀錄.mp4
# Asset_name: 8f2e5e36-d043-4182-9634-xxxxxxxx, file_name: an552Qb_460svvp9.webm
# Asset_name: aef495c1-a3dd-49bb-8e3e-xxxxxxxx, file_name: world_war_2.webm
# Asset_name: b53d8152-6ecd-41a2-a59e-xxxxxxxx, file_name: an552Qb_460svvp9.webm - Media Encoder Standard encoded

However, when I tried to use the following method; it failed. Since I have no idea what to parse as parameters - Link to Python SDKs

create_or_update(resource_group_name, account_name, asset_name,
                 parameters, custom_headers=None, raw=False, **operation_config)


Therefore, I would like to ask questions as follows (everything is done via Python SDKs):

  1. What kind of parameters does it expect?
  2. Can a video be uploaded directly to AMS or it should be uploaded to Blob Storage first?
  3. Should an Asset contain only one video or multiple files are fine?

Solution

  • I have found my method to work around using Python SDKs and REST; however, I am not quite sure it's proper.

    1. Log-In to Azure Media Services and Blob Storage via Python packages

      import adal
      from msrestazure.azure_active_directory import AdalAuthentication
      from msrestazure.azure_cloud import AZURE_PUBLIC_CLOUD
      from azure.mgmt.media import AzureMediaServices
      from azure.mgmt.media.models import MediaService
      
      from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
      
    2. Create Assets for an original file and an encoded one by parsing these parameters. Example of the original file Asset creation.

      asset_name = 'asset-myvideo'
      asset_properties = {
          'properties': {
              'description': 'Original File Description',
              'storageAccountName': "storage-account-name"
          }
      }
      client.assets.create_or_update(RESOUCE_GROUP_NAME, ACCOUNT_NAME, asset_name, asset_properties)
      
    3. Upload a video to the Blob Storage derived from the created original asset

      current_container = [data.container for data in client.assets.list(RESOUCE_GROUP_NAME, ACCOUNT_NAME).get(0) if data.name == asset_name][0] # Get Blob Storage location
      file_name = "myvideo.mp4"
      blob_client = blob_service_client.get_blob_client(container=current_container, blob=file_name)
      
      with open('original_video.mp4', 'rb') as data:
          blob_client.upload_blob(data)
      print(f'Video uploaded to {current_container}')
      

    And after that, I do Transform, Job, and Streaming Locator to get the video Streaming Link successfully.