Skip to content

Instantly share code, notes, and snippets.

@mikesparr
Created January 23, 2023 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikesparr/d73392ddaefc0dd2cc885544ffac1335 to your computer and use it in GitHub Desktop.
Save mikesparr/d73392ddaefc0dd2cc885544ffac1335 to your computer and use it in GitHub Desktop.
Experiment fixing missing file duration metadata using ffmpeg for Google Cloud Platform (GCP) Transcoder API
#!/usr/bin/env bash
#####################################################################
# REFERENCES
# - https://cloud.google.com/transcoder/docs/transcode-video#create-job-from-preset-python
# - https://ffmpeg.org/ffmpeg.html#Stream-copy
#####################################################################
export PROJECT_ID=$(gcloud config get-value project)
export PROJECT_USER=$(gcloud config get-value core/account) # set current user
export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)")
export IDNS=${PROJECT_ID}.svc.id.goog # workflow identity domain
export GCP_REGION="us-central1" # CHANGEME (OPT)
export GCP_ZONE="us-central1-a" # CHANGEME (OPT)
export NETWORK_NAME="default"
# enable apis
gcloud services enable compute.googleapis.com \
storage.googleapis.com
# configure gcloud sdk
gcloud config set compute/region $GCP_REGION
gcloud config set compute/zone $GCP_ZONE
# pull file
export PROB_FILE_NAME="prob-file.mpg"
export REF_FILE_NAME="ref-file.mpeg"
export NEW_FILE_NAME1="copied-file-1.mpg"
export PROB_FILE_URL="https://storage.googleapis.com/lumen5-prod-video/shutterstock997294tprEe9.mpg"
export REF_FILE_URL="https://filesamples.com/samples/video/mpeg/sample_640x360.mpeg"
# pull reference file
curl -o $REF_FILE_NAME $REF_FILE_URL
# pull problem file
curl -o $PROB_FILE_NAME $PROB_FILE_URL
# install local tooling and test
brew install ffmpeg
# test ref file (13.279933)
ffprobe -v error -select_streams v:0 \
-show_entries stream=duration \
-of default=noprint_wrappers=1:nokey=1 $REF_FILE_NAME
# test prob file (N/A)
ffprobe -v error -select_streams v:0 \
-show_entries stream=duration \
-of default=noprint_wrappers=1:nokey=1 $PROB_FILE_NAME
# copy file (using ffmpeg)
ffmpeg -i $PROB_FILE_NAME -vcodec copy -acodec copy $NEW_FILE_NAME1
# test copied file (3.291667)
ffprobe -v error -select_streams v:0 \
-show_entries stream=duration \
-of default=noprint_wrappers=1:nokey=1 $NEW_FILE_NAME1
# (optional) test against transcoder API
@mikesparr
Copy link
Author

Overview

A customer had some Google Cloud Platform (GCP) Transcoder API errors on certain files. It was discovered that these files were missing the video duration in the metadata.

Experiment

  • Fetch problem file
  • Recreate issue and confirm "N/A" appeared when trying to view duration using ffprobe tool
  • Leverage the ffmpeg copy feature and retry
  • Confirm that the duration now exists in file

Result

Screenshot 2023-01-23 at 11 20 17 AM

Cleanup

Be sure if you create any resources on the cloud, to delete them or the GCP project to avoid unnecessary charges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment