npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cordova-plugin-media-editor

v1.0.3

Published

Cordova Media Editor Plugin

Downloads

7

Readme

npm version

This is a cordova plugin to assist in several video editing tasks such as:

  • Transcoding
  • Trimming
  • Creating thumbnails from a video file (now at a specific time in the video)
  • Getting info on a video - width, height, orientation, duration, size, & bitrate.

After looking at an article on How Vine Satisfied Its Need for Speed, it was clear Cordova/Phonegap needed a way to modify videos to be faster for app's that need that speed.

This plugin will address those concerns, hopefully.

Installation

cordova plugin add cordova-plugin-media-editor

CTYMediaEditor will be available in the window after deviceready.

Usage

Transcode a video

// parameters passed to transcodeVideo
CTYMediaEditor.transcodeVideo(
    success, // success cb
    error, // error cb
    {
        fileUri: 'file-uri-here', // the path to the video on the device
        outputFileName: 'output-name', // the file name for the transcoded video
        outputFileType: "MPEG4", // android is always mp4
        optimizeForNetworkUse: 1, // ios only
        saveToLibrary: true, // optional, defaults to true
        deleteInputFile: false, // optional (android only), defaults to false
        maintainAspectRatio: true, // optional, defaults to true
        width: 640, // optional, see note below on width and height
        height: 640, // optional, see notes below on width and height
        videoBitrate: 1000000, // optional, bitrate in bits, defaults to 1 megabit (1000000)
        fps: 24, // optional (android only), defaults to 24
        audioChannels: 2, // optional (ios only), number of audio channels, defaults to 2
        audioSampleRate: 44100, // optional (ios only), sample rate for the audio, defaults to 44100
        audioBitrate: 128000, // optional (ios only), audio bitrate for the video in bits, defaults to 128 kilobits (128000)
        progress: function(info) {} // info will be a number from 0 to 100
    }
);

A note on width and height used by transcodeVideo

I recommend setting maintainAspectRatio to true. When this option is true you can provide any width/height and the height provided will be used to calculate the new width for the output video. If you set maintainAspectRatio false there is a good chance you'll end up with videos that are stretched and/or distorted. Here is the simplified formula used on iOS when maintainAspectRatio is true -

aspectRatio = videoWidth / videoHeight;
outputWidth = height * aspectRatio;
outputHeight = outputWidth / aspectRatio;

Android will always use the aspect ratio of the input video to calculate the scaled output width and height. Setting maintainAspectRatio on android will make not make a difference.

If you don't provide width and height to transcodeVideo the output video will have the same dimensions as the input video.

transcodeVideo example -

// options used with transcodeVideo function // Support's type:

M4V, MP4, MPEG4, M4A, QUICK_TIME, WAV, MP3

// this example uses the cordova media capture plugin
navigator.device.capture.captureVideo(
    videoCaptureSuccess,
    videoCaptureError,
    {
        limit: 1,
        duration: 20
    }
);

function videoCaptureSuccess(mediaFiles) {
    // Wrap this below in a ~100 ms timeout on Android if
    // you just recorded the video using the capture plugin.
    // For some reason it is not available immediately in the file system.

    var file = mediaFiles[0];
    var videoFileName = 'video-name-here'; // I suggest a uuid

    CTYMediaEditor.transcodeVideo(
        videoTranscodeSuccess,
        videoTranscodeError,
        {
            fileUri: file.fullPath,
            outputFileName: videoFileName,
            outputFileType: "MPEG4",
            optimizeForNetworkUse: 1,
            saveToLibrary: true,
            maintainAspectRatio: true,
            width: 640,
            height: 640,
            videoBitrate: 1000000, // 1 megabit
            audioChannels: 2,
            audioSampleRate: 44100,
            audioBitrate: 128000, // 128 kilobits
            progress: function(info) {
                console.log('transcodeVideo progress callback, info: ' + info);
            }
        }
    );
}

function videoTranscodeSuccess(result) {
	// result is the path to the transcoded video on the device
    console.log('videoTranscodeSuccess, result: ' + result);
}

function videoTranscodeError(err) {
	console.log('videoTranscodeError, err: ' + err);
}

Transcode a audio

// parameters passed to transcodeAudio
CTYMediaEditor.transcodeAudio(
    success, // success cb
    error, // error cb
    {
        fileUri: 'file-uri-here', // the path to the audio on the device
        outputFileName: 'output-name', // the file name for the transcoded audio
        outputFileType: "MP3", // android is always mp3
        optimizeForNetworkUse: 1, // ios only
        saveToLibrary: true, // optional, defaults to true
        deleteInputFile: false, // optional (android only), defaults to false    
        audioChannels: 2, // optional (ios only), number of audio channels, defaults to 2
        audioSampleRate: 44100, // optional (ios only), sample rate for the audio, defaults to 44100
        audioBitrate: 128000, // optional (ios only), audio bitrate for the audio in bits, defaults to 128 kilobits (128000)
        progress: function(info) {} // info will be a number from 0 to 100
    }
);

Trim a Video (iOS only)

CTYMediaEditor.trim(
    trimSuccess,
    trimFail,
    {
        fileUri: 'file-uri-here', // path to input video
        trimStart: 5, // time to start trimming in seconds
        trimEnd: 15, // time to end trimming in seconds
        outputFileName: 'output-name', // output file name
        progress: function(info) {} // optional, see docs on progress
    }
);

function trimSuccess(result) {
    // result is the path to the trimmed video on the device
    console.log('trimSuccess, result: ' + result);
}

function trimFail(err) {
    console.log('trimFail, err: ' + err);
}

Create a JPEG thumbnail from a video

CTYMediaEditor.createThumbnail(
    success, // success cb
    error, // error cb
    {
        fileUri: 'file-uri-here', // the path to the video on the device
        outputFileName: 'output-name', // the file name for the JPEG image
        atTime: 2, // optional, location in the video to create the thumbnail (in seconds)
        width: 320, // optional, width of the thumbnail
        height: 480, // optional, height of the thumbnail
        quality: 100 // optional, quality of the thumbnail (between 1 and 100)
    }
);
// atTime will default to 0 if not provided
// width and height will be the same as the video input if they are not provided
// quality will default to 100 if not provided
// this example uses the cordova media capture plugin
navigator.device.capture.captureVideo(
    videoCaptureSuccess,
    videoCaptureError,
    {
        limit: 1,
        duration: 20
    }
);

function videoCaptureSuccess(mediaFiles) {
    // Wrap this below in a ~100 ms timeout on Android if
    // you just recorded the video using the capture plugin.
    // For some reason it is not available immediately in the file system.

    var file = mediaFiles[0];
    var videoFileName = 'video-name-here'; // I suggest a uuid

    CTYMediaEditor.createThumbnail(
        createThumbnailSuccess,
        createThumbnailError,
        {
            fileUri: file.fullPath,
            outputFileName: videoFileName,
            atTime: 2,
            width: 320,
            height: 480,
            quality: 100
        }
    );
}

function createThumbnailSuccess(result) {
    // result is the path to the jpeg image on the device
    console.log('createThumbnailSuccess, result: ' + result);
}

A note on width and height used by createThumbnail

The aspect ratio of the thumbnail created will match that of the video input. This means you may not get exactly the width and height dimensions you give to createThumbnail for the jpeg. This for your convenience but let us know if it is a problem. I am considering adding a maintainAspectRatio option to createThumbnail (and when this option is false you might have stretched, square thumbnails :laughing:).

Get info on a video (width, height, orientation, duration, size, & bitrate)

CTYMediaEditor.getVideoInfo(
    success, // success cb
    error, // error cb
    {
        fileUri: 'file-uri-here', // the path to the video on the device
    }
);
CTYMediaEditor.getVideoInfo(
    getVideoInfoSuccess,
    getVideoInfoError,
    {
        fileUri: file.fullPath
    }
);

function getVideoInfoSuccess(info) {
    console.log('getVideoInfoSuccess, info: ' + JSON.stringify(info, null, 2));
    // info is a JSON object with the following properties -
    {
        width: 1920,
        height: 1080,
        orientation: 'landscape', // will be portrait or landscape
        duration: 3.541, // duration in seconds
        size: 6830126, // size of the video in bytes
        bitrate: 15429777 // bitrate of the video in bits per second
    }
}

Android & FFmpeg

FFmpeg has been removed from android for several reasons but mainly for performance. If you still need the old functionality that FFmpeg provided V1.09 is the last version that will use it.

On iOS

iOS Developer AVFoundation Documentation

Video compression in AVFoundation

AVFoundation slides - tips/tricks

AVFoundation slides #2

Bob McCune's AVFoundation Editor - ios app example

Saving videos after recording videos

On Android

Android Documentation

Android Media Stores

How to Port ffmpeg (the Program) to Android–Ideas and Thoughts

How to Build Android Applications Based on FFmpeg by An Example

Acknowledgments

A huge thank you to jbavari for his pioneering work on cordova-plugin-video-editor project, which was an inspiration and heavy influence on cordova-plugin-media-editor

License

Android: Apache 2.0

iOS: MIT

Windows: Apache 2.0