How to automatically rotate videos with MediaConvert?

0

I am developing an app that supports uploading videos. Videos are transformed using MediaConvert. This is done with the Video On Demand on AWS prebuilt solution.

Some of the videos I upload appear rotated in the output. I saw about setting "Rotate": "AUTO" on my Job description but that had no effect. I saw this post as well, which mentions that sometimes metadata on mobile phones is weird. How can we detect such cases and rotate accordingly? Is that something we can do with a no-code solution, or do we need to write some code for it?

已提问 4 个月前125 查看次数
2 回答
0
已接受的回答

I got this answer but for some reason is not posted here. I copy paste from my email.

To handle video rotation issues when using AWS MediaConvert, you typically need to address the video metadata that dictates the orientation. As you mentioned, mobile devices sometimes save videos with orientation metadata that needs to be interpreted correctly during processing.

Here’s a step-by-step approach to detect and fix video rotation issues, along with whether you can achieve this without code or with minimal coding:

Steps to Address Video Rotation Issues

Understand the Problem: Mobile devices often use metadata to record the orientation in which a video was captured. If MediaConvert doesn’t handle this metadata correctly, the video might appear rotated in the output.

Use the Rotate Option: The "Rotate": "AUTO" option should handle most cases where the rotation metadata is present and correct. However, this might not work if the metadata is absent or incorrect.

Metadata Extraction: To correctly handle video rotation, you might need to extract the metadata and adjust the video accordingly before sending it to MediaConvert.

No-Code Solutions AWS MediaConvert: Using the Rotate option should handle most of the rotation issues. You can set this option in your MediaConvert job settings: json Copia codice { "Settings": { "Input": { "Rotate": "AUTO" } } } If this does not work, a no-code solution may not fully address the issue.

Third-Party Tools: Some third-party video processing tools might offer better handling of orientation metadata as part of their no-code solutions, but this depends on their capabilities.

Low-Code Solutions AWS Lambda and ffmpeg: You can use AWS Lambda functions with ffmpeg to preprocess the video before sending it to MediaConvert. Here’s a basic example:

import subprocess
import boto3
def lambda_handler(event, context):
   # Extract video file details from
   event input_bucket = event['input_bucket']
   input_key = event['input_key']
   output_bucket = event['output_bucket']
   output_key = event['output_key']

   # Download the video file from S3
   s3 = boto3.client('s3')
   input_file_path = '/tmp/input.mp4'
   s3.download_file(input_bucket, input_key, input_file_path)
   # Use ffmpeg to auto-rotate based on metadata
   output_file_path = '/tmp/output.mp4
   subprocess.run(['ffmpeg', '-i', input_file_path, '-vf', 'transpose=1', output_file_path])

   # Upload the rotated video back to S3
   s3.upload_file(output_file_path, output_bucket, output_key)

   return { 'statusCode': 200, 'body': 'Video processed and rotated successfully' }

This Lambda function downloads a video from S3, processes it with ffmpeg to auto-rotate based on metadata, and then uploads the processed video back to S3.

Advanced Code Solutions Custom Metadata Processing: You can write more advanced code to extract the orientation metadata using libraries such as pymediainfo and adjust the video rotation accordingly.

from pymediainfo import MediaInfo
import subprocess
import boto3

def lambda_handler(event, context):
  input_bucket = event['input_bucket']
  input_key = event['input_key']
  output_bucket = event['output_bucket']
  output_key = event['output_key']
  s3 = boto3.client('s3')
  input_file_path = '/tmp/input.mp4'
  s3.download_file(input_bucket, input_key, input_file_path)
  media_info = MediaInfo.parse(input_file_path)
  rotation = None
  for track in media_info.tracks:
    if track.track_type == "Video": rotation = track.rotation
    break
  output_file_path = '/tmp/output.mp4'
  
  if rotation:
    if rotation == "90":
      transpose_value = 1
    elif rotation == "180":
      transpose_value = 2
    elif rotation == "270":
      transpose_value = 3
    else: transpose_value = None
    
    if transpose_value is not None:
      subprocess.run(['ffmpeg', '-i', input_file_path, '-vf', f'transpose={transpose_value}', output_file_path])
    else: subprocess.run(['ffmpeg', '-i', input_file_path, output_file_path])
  else:
    subprocess.run(['ffmpeg', '-i', input_file_path, output_file_path])
    
  s3.upload_file(output_file_path, output_bucket, output_key)
  
  return { 'statusCode': 200, 'body': 'Video processed and rotated successfully' } 

While using Rotate: AUTO in AWS MediaConvert is a good start, handling more complex cases where metadata is absent or incorrect will likely require some coding. The examples provided using AWS Lambda and ffmpeg can help preprocess videos to correct orientation issues before they are processed by MediaConvert. This approach ensures that your videos are correctly rotated regardless of the source metadata quality.

已回答 3 个月前
0

You can use step functions prior to trigger your job with MediConvert to inspect the video resolutions. One way to accomplish this is using ffprobe that will inspect the video resolution, with a lambda function that triggers a similar command like the one bellow: ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 input_file

Once you know the resolution your step function can decide if the input file needs to be rotated during the transcode. Another way is to check if the metadata for display matrix rotation is available: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '20230531_083540.mp4': ... Side data: displaymatrix: rotation of -180.00 degrees

AWS
Nuno_Q
已回答 4 个月前
profile picture
专家
已审核 4 个月前
  • That approach means I have to write code for it, right? Does that mean that it would be the best approach?

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则