Simple example add sequencent image with difrent long time

edzioedzio77 wrote on 11/16/2018, 11:29 AM

I have been trying to write a simple script for 3 days.The script will load photos from the folder

d:\photo

The photos are name
 

1.png 
2.png 
3.png 
4.png 
...
9.png
10.png

Each photo should last as much time as the corresponding number in the table of numbers, for example

1 second
1 second
1 second
2 seconds
...
1 second
1 second
14 seconds

I tried to write something like that

Based on the manual and other sources, I wrote this:

using System.Windows.Forms;
using Sony.Vegas;                // old Vegas
//using ScriptPortal.Vegas;       // VEGASA 14 or 15

using System;            // need to String
using System.Text;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

public class EntryPoint {
  
    public void FromVegas(Vegas vegas) {
        AddVideoEvent(vegas, "\"D:\photo\\1.png\"", Timecode.FromSeconds(2), Timecode.FromSeconds(2));
        MessageBox.Show("hello world");
    }
    VideoEvent AddVideoEvent(Vegas vegas, String mediaFile, Timecode start, Timecode length)
    {
        Media media = new Media(mediaFile);
        VideoTrack track = vegas.Project.AddVideoTrack();
        VideoEvent videoEvent = track.AddVideoEvent(start, length);
        Take take = videoEvent.AddTake(media.GetVideoStreamByIndex(0));
        return videoEvent;
    }
}

Unfortunately, this does not work an error occurs

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Media stream not specified.
   at Sony.Vegas.Take.AddSelf(UInt32 trackID, Int64 eventID, MediaType mediaType)
   at Sony.Vegas.TrackEvent.AddTake(MediaStream mediaStream)
   at EntryPoint.AddVideoEvent(Vegas vegas, String mediaFile, Timecode start, Timecode length)
   at EntryPoint.FromVegas(Vegas vegas)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Sony.Vegas.ScriptHost.ScriptManager.Run(Assembly asm, String className, String methodName)
   at Sony.Vegas.ScriptHost.RunScript(Boolean fCompileOnly)

 

Comments

klt wrote on 11/16/2018, 12:53 PM

I'm new to scripting.

"\"D:\photo\\1.png\""

Can you explain me this string? Why the slashes, why the doubleslash?

edzioedzio77 wrote on 11/16/2018, 2:05 PM

In C, the quote is a special character. In order for STRING to contain quotes, it must be preceded by a backslash. Similarly, to write a backslash it must be duplicated.

example:

https://stackoverflow.com/questions/5540701/how-to-use-a-string-with-quotation-marks-inside-it

 

klt wrote on 11/16/2018, 2:11 PM

Thank you.

In this case I'd expect the string look like

"\"D:\\photo\\1.png\""

(doubleslash after the drive letter too)

edzioedzio77 wrote on 11/16/2018, 2:21 PM

Yes, but this still doesn't work :-(

klt wrote on 11/16/2018, 3:54 PM

Does it work for you so?

 

 public void FromVegas(Vegas vegas) {
        AddVideoEvent(vegas, "D:\\photo\\1.png", Timecode.FromSeconds(2), Timecode.FromSeconds(2));
        MessageBox.Show("hello world");
    }
    VideoEvent AddVideoEvent(Vegas vegas, String mediaFile, Timecode start, Timecode length)
    {
        Media media = Media.CreateInstance(Project.ActiveProject, mediaFile);
        VideoTrack track = vegas.Project.AddVideoTrack();
        VideoEvent videoEvent = track.AddVideoEvent(start, length);
        //Take take = videoEvent.AddTake(media.GetVideoStreamByIndex(0));
        return videoEvent;
    }

 

edzioedzio77 wrote on 11/16/2018, 4:06 PM

YUPI

After many moments of frustration I found a solution.

 

using System.Windows.Forms;
using Sony.Vegas;           
//using ScriptPortal.Vegas;         //for VEGAS ver 14 or 15 or 16
using System;                     // This include String Type
using System.Text;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
public class EntryPoint {   
    int n = 3;
    int ii;
    String  dir = "d:\\photo\\";
    String     [] name_file  = new String[]  {"10.png",
                                              "12.png",
                                              "14.png"
                                             };
    VideoEvent [] videoEvent = new VideoEvent[3];
    int        [] length     = new int[] {2, 4, 7};
    int offset = 0;
        
    public void FromVegas(Vegas vegas)
    {
        VideoTrack track = vegas.Project.AddVideoTrack(); // Create viedo track
        for( ii=0; ii<n; ii++)
        {
            videoEvent[ii] = AddVideoEvent(vegas, dir+name_file[ii],
                                           Timecode.FromSeconds(offset),
                                           Timecode.FromSeconds(length[ii]),  
                                           track);
            offset = offset + length[ii];
        }
    }
    VideoEvent AddVideoEvent(Vegas vegas, String mediaFile, Timecode start, Timecode length, VideoTrack track)
    {
        Media media = new Media(mediaFile);
        VideoEvent videoEvent = track.AddVideoEvent(start, length);     // Create event in given place and given length
        Take take = videoEvent.AddTake(media.GetVideoStreamByIndex(0)); // Fills the event with content
       return videoEvent;                                               // return handle
    }
}

 

klt wrote on 11/16/2018, 4:22 PM

After some errors I found that basically you don't need the quotes in the string. :)

And I had to change

using Sony.Vegas; ---> using ScriptPortal.Vegas;

Are you using and old version of Vegas?

edzioedzio77 wrote on 11/16/2018, 4:34 PM

Yes, I use 13