Limit Duration of MP4/MOV Files

Instructions

  1. Install our free Custom Javascript for Gravity Forms plugin. Download the plugin here: https://gravitywiz.com/gravity-forms-code-chest/
    1. Copy and paste the snippet into the editor of the Custom Javascript for Gravity Forms plugin.

Code

Filename: gpfup-limit-duration-of-mp4-mov-files.js

/**
 * Gravity Perks // File Upload Pro // Limit Duration of MP4/MOV Files
 * https://gravitywiz.com/documentation/gravity-forms-file-upload-pro/
 *
 * Instructions:
 *   1. Install our free Custom Javascript for Gravity Forms plugin.
 *      Download the plugin here: https://gravitywiz.com/gravity-forms-code-chest/
 *   2. Copy and paste the snippet into the editor of the Custom Javascript for Gravity Forms plugin.
 */
window.gform.addAction('gpfup_before_upload', (formId, fieldId, file, up, gpfupInstance) => {
	// Update "10" to the desired number of seconds.
	var maxDurationInSeconds = 10;

	const videoMimeTypes = ['video/mp4', 'video/quicktime'];

	if (videoMimeTypes.indexOf(file.type) !== -1) {
		var fileURL = URL.createObjectURL(file.getNative());
		var vid = document.createElement('video');
		vid.src = fileURL;

		// wait for duration to change from NaN to the actual duration
		vid.ondurationchange = function () {
			if (this.duration > maxDurationInSeconds) {
				gpfupInstance.handleFileError(up, file, {
					code: 'does_not_meet_max_duration',
					message: 'Video duration cannot exceed  ' + maxDurationInSeconds + ' seconds.',
				});
			}
		};
	}
});

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.