Activity › Forums › Adobe After Effects Expressions › Identifying a Video or image ExtendScript
-
Identifying a Video or image ExtendScript
Posted by Avinash Ramanath on September 21, 2018 at 1:11 pmHello,
Am trying to import a video file or image and move it to a folder called “Videos”. But am unable to do so. Could you please have a look.var myPath = "/Users/avinashramanath/Desktop/Video/1.mp4";
newFootageItem = app.project.importFile(new ImportOptions(new File(myPath)));
var compFolder = app.project.items.addFolder("Videos");
for(var i = 1; i <= app.project.numItems; i++) {
if(app.project.item(i).hasVideo == true && item.hasAudio == true && item.duration != 0)app.project.item(i).parentFolder = compFolder ;
}
Avinash Ramanath replied 6 years, 2 months ago 3 Members · 10 Replies -
10 Replies
-
James Ronan
September 21, 2018 at 2:19 pmIf that’s all you want to do, you don’t need to loop through all the items in the project:
var myPath = "/Users/avinashramanath/Desktop/Video/1.mp4";newFootageItem = app.project.importFile(new ImportOptions(new File(myPath)));
var compFolder = app.project.items.addFolder("Videos");
newFootageItem.parentFolder = compFolder ;
-
Avinash Ramanath
September 21, 2018 at 2:43 pmYes thanks James. That worked. But I’m still wondering how to identify a video file or an image file via scripting.
For example a comp can be identified by instanceof CompItem. -
Avinash Ramanath
September 21, 2018 at 3:55 pmCould you please tell me why the below code is omitting files with odd number names. Am very new to scripting and figuring out a lot of things
{
var targetFolder = File("/Users/avinashramanath/Desktop/Video");if (targetFolder) {
var files = targetFolder.getFiles();
for (var i = 0; i < files.length; i++)
{
try {
var importOptions = new ImportOptions (files[i]);
app.project.importFile (importOptions);
} catch (error) { /*alert(error.toString());*/}
}
}
}{
var compFolder = app.project.items.addFolder("Assets");for(var i = 1; i <= app.project.numItems; i++)
{if(app.project.item(i) instanceof FootageItem)
app.project.item(i).parentFolder = compFolder;
}
}
-
James Ronan
September 22, 2018 at 8:18 amHere is a basic example of determining whether it’s an image or video:
It didn’t omit odd file names for me.var targetFolder = File("/Users/avinashramanath/Desktop/Video");
var compFolder = app.project.items.addFolder("Assets");if (targetFolder) {
var files = targetFolder.getFiles();
for (var i = 0; i <= files.length; i++) {
try {
var importOptions = new ImportOptions(files[i]);
var newItem = app.project.importFile(importOptions);if (newItem instanceof FootageItem && !newItem.mainSource.isStill) { // Check if Video or image
newItem.parentFolder = compFolder;
}
} catch (error) { /*alert(error.toString());*/ }
}
} -
Avinash Ramanath
September 22, 2018 at 4:38 pmI don’t know what am doing wrong here, must be in the for loop. The script seems to be selecting files with odd number names and only placing those inside the Assets folder. Please check the image to understand my problem,
// Select folder to import
var targetFolder = Folder.selectDialog("Import Files From Folder");
if (targetFolder)
{
var files = targetFolder.getFiles();
for (var i = 0; i < files.length; i++)
{
try
{
var importOptions = new ImportOptions(files[i]);
app.project.importFile(importOptions);
}
catch (error)
{ /*alert(error.toString());*/ }
}
}
//create Assets folder
var compFolder = app.project.items.addFolder("Assets");
//collect items and move to Assets folder
var files = app.project.items.length;
for (var i = 1; i <= files; i++)
{
if (app.project.item(i).hasVideo == true && app.project.item(i).hasAudio == false && app.project.item(i).duration == 0)
{ // Check if Video or image
app.project.item(i).parentFolder = compFolder;
}
else
if (app.project.item(i).hasVideo == true && app.project.item(i).hasAudio == true && app.project.item(i).duration !== 0)
{ // Check if Video or image
app.project.item(i).parentFolder = compFolder;
}
} -
Avinash Ramanath
September 22, 2018 at 4:39 pm -
Dan Ebberts
September 22, 2018 at 5:18 pmYou’re changing the order of the items while you’re looping through them. I would loop through all the items and store the ones that need to be moved into an array. Then loop through the array and move them into the compFolder.
Dan
-
Dan Ebberts
September 22, 2018 at 6:09 pmI haven’t tested it, but here you go:
// Select folder to import
var targetFolder = Folder.selectDialog("Import Files From Folder");
if (targetFolder)
{
var files = targetFolder.getFiles();
for (var i = 0; i < files.length; i++)
{
try
{
var importOptions = new ImportOptions(files[i]);
app.project.importFile(importOptions);
}
catch (error)
{ /*alert(error.toString());*/ }
}
}
//create Assets folder
var compFolder = app.project.items.addFolder("Assets");
//collect items and move to Assets folder
var files = app.project.items.length;
var myItems = [];
for (var i = 1; i <= files; i++)
{
if (app.project.item(i).hasVideo == true && app.project.item(i).hasAudio == false && app.project.item(i).duration == 0)
{ // Check if Video or image
myItems.push(app.project.item(i));
}
else
if (app.project.item(i).hasVideo == true && app.project.item(i).hasAudio == true && app.project.item(i).duration !== 0)
{ // Check if Video or image
myItems.push(app.project.item(i));
}
}
for (var i = 0; i < myItems.length; i++){
myItems[i].parentFolder = compFolder;
}
Dan
-
Avinash Ramanath
September 22, 2018 at 6:13 pmYes Dan, it works fine now. Thanks to you James Ronan. I have learned a new thing now.
Reply to this Discussion! Login or Sign Up