css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <section class="bcls-section" id="video_data">
  <h2>Ingesting Low Priority Videos</h2>
  <h3>Video data</h3>

  <p>This script expects a data set as a JSON array of objects like the one below. Paste your own data into the field if you are using your own account. You may add additional fields to the objects for the videos such as <code>description</code>, <code>tags</code>,
    or <code>reference_id</code>, and these will be passed through to the CMS API on the <strong>create video request.</strong>. Be sure you get the syntax right and only include fields that are allowed for a <a href="https://brightcovelearning.github.io/Brightcove-API-References/cms-api/v1/doc/index.html#api-videoGroup-Create_Video">Create Video</a>    request.</p>
  <p>The only fields <strong>required</strong> by this app are the <code>name</code> and <code>url</code> (for the video source file).</p>

  <p>Video data:<textarea id="videoData" class="bcls-code" style="height:10em;">[
      {
          "name": "Great White Egret",
          "url": "https://learning-services-media.brightcove.com/videos/mp4/Great_White_Egret_4K.mp4"
      },
      {
          "name": "Swan at Sizergh Castle",
          "url": "https://learning-services-media.brightcove.com/videos/mp4/Adult-and-young-swan-Sizergh-Castlle-Cumbria.mov"
      },
      {
          "name": "Great Blue Heron",
          "url": "http://learning-services-media.brightcove.com/videos/mp4/greatblueheron.mp4"
      },
      {
          "name": "Great Horned Owl",
          "url": "http://learning-services-media.brightcove.com/videos/mp4/greathornedowl.mp4"
      }
  ]
</textarea></p>

  <div class="form-wrapper">
    <fieldset class="bcls-fieldset">
      <legend>Inputs for Dynamic Ingest</legend>

      <p>Account: (defaults to Brightcove sample account) <input id="account_id" type="text" value="" name="account_id" /></p>

      <p>Client id: (defaults to Brightcove sample account) <input id="client_id" type="text" value="" name="client_id" /></p>

      <p>Client secret: (defaults to Brightcove sample account) <input id="client_secret" type="text" value="" name="client_secret" /></p>

      <p>Ingest profile: <select id="ingest_profile_display"></select><br /> or Custom Profile: <input id="custom_profile_display" type="text" value="" name="custom_profile_display" /> (you must use the name exactly as it appears in the Video Cloud system,
        or the request will fail)</p>

      <p><input id="di_Submit" class="bcls-button" type="submit" value="Start Ingest" /></p>

  </div>
  <p>Full request URL for the CMS API call:<br />
    <textarea class="bcls-code" name="url" id="cms_url"style="min-height:1em;"></textarea> (automatically generated - for information only)</p>

  <p>Full request URL for the DI API call:<br />
    <textarea class="bcls-code" name="url" id="di_url" style="min-height:1em;"></textarea> (automatically generated - for information only)</p>

  </fieldset>
  </div>
</section>

<section class="bcls-section">
  <h2>Results Log</h2>
  <p>Log messages will appear here:</p>
  <div id="logger" class="logger" id="logger"></div>
    <pre class="line-numbers">
<code id="apiResponse" class="language-json">Response will appear here...</code></pre>
</section>

            
          
!
            
              /* * The body style is just for the
 * background color of the codepen.
 * Do not include in your code.
 */
body {
  background-color: #111;
  color: #f5f5f5;
  font-family: sans-serif;
  margin: 2em;
}
/*
 * Styles essential to the sample
 * are below
 */
.bcls-code {
  color: #111;
  font-family: 'Source Code Pro',monospace;
  padding: 1em;
  width: 90%;
  min-height: 5em;
  background-color: #cfcfcf;
  font-size: 1rem;
}
.bcls-button {
  padding: .5em;
  background-color: #333;
  border: 1px yellow solid;
  border-radius: .5em;
  color: yellow;
  cursor: pointer;
}
fieldset {
  color: #64AAB2;
  border: 1px #64AAB2 solid;
  border-radius: .5em;
}
code {
  color: #E4B842;
  font-family: 'Source Code Pro',monospace;
}

a {
  color: RGB(249, 218, 126);
}

.logger {
  color: RGB(249, 218, 126);
}
            
          
!
            
              var BCLS = (function(window, document) {
  var account_id_display = document.getElementById('account_id'),
    account_id,
    client_id_display = document.getElementById('client_id'),
    client_id,
    client_secret_display = document.getElementById('client_secret'),
    client_secret,
    ingest_profile_display = document.getElementById('ingest_profile_display'),
    ingest_profile,
    custom_profile_display = document.getElementById('custom_profile_display'),
    cms_url_display = document.getElementById('cms_url'),
    videoDataDisplay = document.getElementById('videoData'),
    // Dynamic Ingest API stuff
    profilesArray = ['multi-platform-extended-static', 'multi-platform-standard-static'],
    di_url_display = document.getElementById('di_url'),
    di_submit_display = document.getElementById('di_Submit'),
    proxyURL = 'https://solutions.brightcove.com/bcls/bcls-proxy/bcls-proxy-v2.php',
    apiResponse = document.getElementById('apiResponse'),
    logger = document.getElementById('logger'),
    current_profiles = [],
    current_video_id,
    rawVideoData = [],
    videoData = [],
    totalVideos = 0,
    videoNumber = 0,
    currentJobs = 0,
    totalIngested = 0,
    defaults = {
      account_id: 57838016001
    },
    callbacks = ["http://solutions.brightcove.com/bcls/dynamic-delivery/di-callbacks.php"];


  /**
   * tests for all the ways a variable might be undefined or not have a value
   * @param {*} x the variable to test
   * @return {Boolean} true if variable is defined and has a value
   */
  function isDefined(x) {
    if (x === '' || x === null || x === undefined) {
      return false;
    }
    return true;
  }

  /**
   * tests whether a variable is an array
   * @param {*} a the variable to test
   * @return {Boolean} true if variable is an array
   */
  function isArray(a) {
    if (Array.isArray(a)) {
      return true;
    }
    return false;
  }

  /*
   * tests to see if a string is json
   * @param {String} str string to test
   * @return {Boolean}
   */
  function isJson(str) {
      try {
          JSON.parse(str);
      } catch (e) {
          return false;
      }
      return true;
  }

  /**
   * get selected value for single select element
   * @param {htmlElement} e the select element
   * @return {String} selected value
   */
  function getSelectedValue(e) {
    var selected = e.options[e.selectedIndex],
      val = selected.value,
      txt = selected.textContent,
      idx = e.selectedIndex;
    return val;
  }

  /**
   * Just a simple logger to inject messages into the page
   * it requires an element with id="logger" referenced as logger
   * @param  {string} m the message to display
   */
  function LogNotification(m) {
    var hr = document.createElement('hr');
    logger.appendChild(hr);
    logger.appendChild(document.createTextNode(m));
  }

  /**
   * determines whether specified item is in an array
   * @param {array} array to check
   * @param {string} item to check for
   * @return {boolean} true if item is in the array, else false
   */
  function arrayContains(arr, item) {
    var i,
      iMax = arr.length;
    for (i = 0; i < iMax; i++) {
      if (arr[i] === item) {
        return true;
      }
    }
    return false;
  }

  /**
   * remove all spaces and line breaks from a string
   * @param {string} a the string to operate on
   * @return {string} the string with spaces and line breaks removed
   */
  function removeSpaceAndBreaks(str) {
    var regex = new RegExp('/\r?\n|\r/g');
    return str.replace(regex, '');
  }



  /**
   * find index of an object in array of objects
   * based on some property value
   *
   * @param {array} targetArray array to search
   * @param {string} objProperty object property to search
   * @param {string} value of the property to search for
   * @return {integer} index of first instance if found, otherwise returns -1
   */
  function findObjectInArray(targetArray, objProperty, value) {
    var i, totalItems = targetArray.length,
      objFound = false;
    for (i = 0; i < totalItems; i++) {
      if (targetArray[i][objProperty] === value) {
        objFound = true;
        return i;
      }
    }
    if (objFound === false) {
      return -1;
    }
  }

  /**
   * remove or add obsolete profiles from the current profiles list
   */
  function toggleObsoleteProfiles() {
    // below are the obsolete profiles - you just have to know their names
    var deprecated_profiles = ['balanced-nextgen-player', 'Express Standard', 'mp4-only', 'balanced-high-definition', 'low-bandwidth-devices', 'balanced-standard-definition', 'single-rendition', 'Live - Standard', 'high-bandwidth-devices', 'Live - Premium HD', 'Live - HD', 'videocloud-default-trial', 'screencast'];
    i = current_profiles.length;
    while (i > 0) {
      i--;
      if (arrayContains(deprecated_profiles, current_profiles[i].name)) {
        current_profiles.splice(i, 1);
      }
    }
    return;
  }

  /**
   * displays the filtered list of profiles
   */
  function displayFilteredProfiles() {
    var option,
      fragment = document.createDocumentFragment(),
      i,
      iMax;
    // add first option
    option = document.createElement('option');
    option.textContent = 'Use account default profile';
    option.value = '';
    fragment.appendChild(option);
    // now add the rest
    iMax = current_profiles.length;
    for (i = 0; i < iMax; i++) {
      option = document.createElement('option');
      option.textContent = current_profiles[i].name;
      option.value = current_profiles[i].name;
      fragment.appendChild(option);
    }
    ingest_profile_display.innerHTML = '';
    ingest_profile_display.appendChild(fragment);
    return;
  }

  function processVideoData(rawVideoData) {
    var i, iMax, field;
    iMax = rawVideoData.length;
    for (i = 0; i < iMax; i++) {
      videoData[i] = {};
      videoData[i].createVideoBody = {};
      videoData[i].ingestVideoBody = {};
      // create video request body
      for (field in rawVideoData[i]) {
        if (field !== 'url') {
          videoData[i].createVideoBody[field] = rawVideoData[i][field];
        }
      }
      // now ingest request body
      if (isDefined(ingest_profile)) {
        videoData[i].ingestVideoBody.profile = ingest_profile;
      }
      videoData[i].ingestVideoBody.priority = 'low';
      videoData[i].ingestVideoBody.master = {};
      videoData[i].ingestVideoBody.master.url = rawVideoData[i].url;
      videoData[i].ingestVideoBody['capture-images'] = true;
      videoData[i].ingestVideoBody.callbacks = callbacks;
    }
    totalVideos = videoData.length;
  }

  function createRequest(type) {
    var options = {},
      reqBody = {};
    if (isDefined(client_id) && isDefined(client_secret)) {
      options.client_id = client_id;
      options.client_secret = client_secret;
    }
    options.proxyURL = proxyURL;
    switch (type) {
      case 'getProfiles':
        var i;
        endpoint = '/profiles';
        options.url = 'https://ingestion.api.brightcove.com/v1/accounts/' + account_id + endpoint;
        options.requestType = 'GET';
        makeRequest(options, function(response) {
          if (isJson(response)) {
            current_profiles = JSON.parse(response);
            toggleObsoleteProfiles();
            // hide legacy profiles
            i = current_profiles.length;
            while (i > 0) {
              i--;
              if (!current_profiles[i].hasOwnProperty('dynamic_origin')) {
                current_profiles.splice(i, 1);
              }
            }

            displayFilteredProfiles();
          } else {
            apiResponse.textContent = response;
            LogNotification('The get all profiles operation failed; see the API Response for the error');
            return;
          }
        });
        break;
      case 'createVideo':
        options.requestBody = JSON.stringify(videoData[videoNumber].createVideoBody);
        options.url = 'https://cms.api.brightcove.com/v1/accounts/' + account_id + '/videos';
        options.requestType = 'POST';
        cms_url_display.value = options.url;
        makeRequest(options, function(response) {
          // parse response
          response = JSON.parse(response);
          apiResponse.textContent = JSON.stringify(response, null, '  ');
          // errors will come back as array
          if (isArray(response)) {
            LogNotification('An error occurred. Look the current API response below, correct the issue with the video data, and try again');
          } else {
            current_video_id = response.id;
            createRequest('ingestVideo');
          }
        });
        break;
      case 'ingestVideo':
        options.url = 'https://ingest.api.brightcove.com/v1/accounts/' + account_id + '/videos/' + current_video_id + '/ingest-requests';
        di_url_display.textContent = options.url;
        options.requestType = 'POST';
        options.requestBody = JSON.stringify(videoData[videoNumber].ingestVideoBody);
        di_url_display.value = options.url;
        makeRequest(options, function(response) {
          // parse response
          response = JSON.parse(response);
          apiResponse.textContent = JSON.stringify(response, null, '  ');
          if (isArray(response)) {
            // an error occurred
            LogNotification('An error occurred. Look the current API response below, correct the issue with the video data, and try again');
          } else {
            videoNumber++;
            currentJobs++;
            if (videoNumber < totalVideos) {
              LogNotification('Processing video number ' + videoNumber);
              createRequest('createVideo');
            } else {
              LogNotification('All jobs submitted');
            }
          }
        });
        break;
      default:
        console.log('bad type - shouldn\'t be here: ', type);
    }
  }

  /**
   * send API request to the proxy
   * @param  {Object} options for the request
   * @param  {String} options.url the full API request URL
   * @param  {String="GET","POST","PATCH","PUT","DELETE"} requestData [options.requestType="GET"] HTTP type for the request
   * @param  {String} options.proxyURL proxyURL to send the request to
   * @param  {String} options.client_id client id for the account (default is in the proxy)
   * @param  {String} options.client_secret client secret for the account (default is in the proxy)
   * @param  {JSON} [options.requestBody] Data to be sent in the request body in the form of a JSON string
   * @param  {Function} [callback] callback function that will process the response
   */
  function makeRequest(options, callback) {
    var httpRequest = new XMLHttpRequest(),
      response,
      proxyURL = options.proxyURL,
      // response handler
      getResponse = function() {
        try {
          if (httpRequest.readyState === 4) {
            if (httpRequest.status >= 200 && httpRequest.status < 300) {
              response = httpRequest.responseText;
              // some API requests return '{null}' for empty responses - breaks JSON.parse
              if (response === '{null}') {
                response = null;
              }
              // return the response
              callback(response);
            } else {
              alert('There was a problem with the request. Request returned ' + httpRequest.status);
            }
          }
        } catch (e) {
          alert('Caught Exception: ' + e);
        }
      };
    /**
     * set up request data
     * the proxy used here takes the following request body:
     * JSON.stringify(options)
     */
    // set response handler
    httpRequest.onreadystatechange = getResponse;
    // open the request
    httpRequest.open('POST', proxyURL);
    // set headers if there is a set header line, remove it
    // open and send request
    httpRequest.send(JSON.stringify(options));
  }

  // event listeners
  videoDataDisplay.addEventListener('click', function() {
    this.select();
  });

  account_id_display.addEventListener('blur', function() {
    if (isDefined(account_id_display.value) && isDefined(client_id_display.value) && isDefined(client_secret_display.value)) {
      // refresh Profiles
      account_id = account_id_display.value;
      client_id = client_id_display.value;
      client_secret = client_secret_display.value;
      // get account profiles
      createRequest('getProfiles');
    }
  });

  client_id_display.addEventListener('blur', function() {
    if (isDefined(account_id_display.value) && isDefined(client_id_display.value) && isDefined(client_secret_display.value)) {
      // refresh Profiles
      account_id = account_id_display.value;
      client_id = client_id_display.value;
      client_secret = client_secret_display.value;
      // get account profiles
      createRequest('getProfiles');
    }
  });

  client_secret_display.addEventListener('blur', function() {
    if (isDefined(account_id_display.value) && isDefined(client_id_display.value) && isDefined(client_secret_display.value)) {
      // refresh Profiles
      account_id = account_id_display.value;
      client_id = client_id_display.value;
      client_secret = client_secret_display.value;
      // get account profiles
      createRequest('getProfiles');
    }
  });



  di_submit_display.addEventListener('click', function() {
    var data = removeSpaceAndBreaks(videoDataDisplay.value);
    // in case of stop/start, reset videoNumber to 0
    videoNumber = 0;
    // get account inputs
    account_id = isDefined(account_id_display.value) ? account_id_display.value : defaults.account_id;
    client_id = client_id_display.value;
    client_secret = client_secret_display.value;
    ingest_profile = getSelectedValue(ingest_profile_display);
    processVideoData(JSON.parse(data));
    cms_url_display.value = 'https://cms.api.brightcove.com/v1/accounts/' + account_id + '/videos';
    // set CMS API options for first video
    createRequest('createVideo');
  });
  // initialize
  function init() {
    // default account id
    account_id = defaults.account_id;
    // get account profiles
    createRequest('getProfiles');
  }
  // call init to set things up
  init();
})(window, document);

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console