Base64 Encoding

When passing video to the Video Intelligence API, you can either pass a Google Cloud Storage URI, or you can embed video data directly within the request's content field, which must be base64 encoded.

Embedding Base64 encoded video

Video data is binary data. Within a gRPC request, you can simply write the binary data out directly; however, JSON is used when making a REST request. JSON is a text format that does not directly support binary data, so you will need to convert such binary data into text using Base64 encoding.

To base64 encode a video file:

Linux

Encode the video file using the base64 command line tool, making sure to prevent line-wrapping by using the -w 0 flag:

    $ base64 source_video_file -w 0 > dest_text_file

Mac OSX

Encode the video file using the base64 command line tool:

    $ base64 source_video_file > dest_text_file

Windows

Encode the video file using the Base64.exe tool:

    C:> Base64.exe -e source_video_file > dest_text_file

The base64-encoded data is written out to the specified destination file. Pass the data in a Video Intelligence API request as the value of the content field:

{
  "requests":[
    {
      "image":{
        "content": "ZkxhQwAAACIQABAAAAUJABtAA+gA8AB+W8FZndQvQAyjv..."
      },
      "features": [
        {
          "type":"LABEL_DETECTION",
          "maxResults":1
        }
      ]
    }
  ]
}

Embedding video content programmatically

Copying encoded video data manually into your requests is not always practical; the preferred method is to embed base64-encoded files into client code. All supported programming languages have built-in mechanisms for base64-encoding content:

Python

In Python, base64 encode video files as follows:

# Import the base64 encoding library.
import base64

# Pass the video data to an encoding function.
def encode_video(video):
  video_content = video.read()
  return base64.b64encode(video_content)

Node.js

In Node.js, base64 encode video files as follows:

// Read the file into memory.
var fs = require('fs');
var videoFile = fs.readFileSync('/path/to/file');

// Convert the video data to a Buffer and base64 encode it.
var encoded = new Buffer(videoFile).toString('base64');

Java

In Java, you can base64 encode image files as follows:

// Import the Base64 encoding library.
import org.apache.commons.codec.binary.Base64;

// Encode the video.
byte[] videoData = Base64.encodeBase64(videoFile.getBytes());