Determine if a ClipProjectIem is a Video or Audio file?

Hi,

What is the best way to determine if a Clip is an audio or video file ?

It seems like the ClipProjectItem should have a property of method to get that info (expl: ClipProjectItem::getMediaType() ).
But no such luck it seems ( A bit odd that it is not there - since other similar ones exist for other type - such as ClipProjectItem::getContentType)

I looked into a few hacks and settle on one for now ( ClipProjectItem::getFootageInterpretation()::getFrameRate() > 0 => Video. Not quite true as image would also return a non zero value but good enough for now)

But since there the “MediaType” constant exists (Constants) and seem to be exactly for this kind of scenario, code should probably be able retrieved directly from the ClipProjectItem object via a defined API.

Or did I miss an property / method somewhere ?

1 Like

You didn’t miss anything; here’s how you could detect that, today.

const { constants } = require("premierepro");

function hasMediaType(clipProjectItem, mediaType) {
  try {
    return !!clipProjectItem.getComponentChain(mediaType);
  } catch {
    return false;
  }
}

const hasVideo = hasMediaType(clipProjectItem, constants.MediaType.VIDEO);
const hasAudio = hasMediaType(clipProjectItem, constants.MediaType.AUDIO);
1 Like

I did try something similar originally but it did not quite work as I was expecting - I think my mistake was to conflate “is” and “has” mediaType

(BTW I think you are missing the await on your code - since the !! would always cast a Promise into a true no ?)

So to check if a file is an audio file, code could be extended to:

const { constants } = require("premierepro");

async function hasMediaType(clipProjectItem, mediaType) {
  try {
    return (await clipProjectItem.getComponentChain(mediaType)) != null;
  } catch {
    return false;
  }
}

async function isVideoClip(clipProjectItem) {
    return await hasMediaType(clipProjectItem, constants.MediaType.VIDEO);
}


async function isAudioClip(clipProjectItem) {
    const audio = await hasMediaType(clipProjectItem, constants.MediaType.AUDIO);
    const video = await hasMediaType(clipProjectItem, constants.MediaType.VIDEO);
    return audio && ! video;
}

const isVideo = await isVideoClip(clipProjectItem);
const isAudio = await isAudioClip(clipProjectItem);

Thanks Bruce !