Skip to main content
Known Participant
October 31, 2012
Question

AE using socket to import picture from URL - Need Your Help

  • October 31, 2012
  • 2 replies
  • 4462 views

I have a project that works with local network using the path name. I want to update the project to replace all placeholders with a picture located at a URL address instead of the local network. I have no experience with socket coding and can't seem to find the right snippets of code on the internet to get me through this point of using URL as a file location.

I tried this code (using a picture at this URL as an example);

webConnect= new Socket;

if(webConnect.open("http://distilleryimage3.s3.amazonaws.com")) {

         webConnect.write('GET /2940b7e0236511e2af9022000a1f9a23_7.jpg HTTP/1.0nn');

          response=  webConnect.read();

          webConnect.close();

          alert('complete');

} else {

         alert('fail');

          }

Any help will be appreciated. I need the results of the socket connection to be a object I can use in the statement

curItem.replace(new File( <picture path> );

Thanks again.

This topic has been closed for replies.

2 replies

stevelewisza10174892
Participant
February 7, 2018

I have found a solution to this for anyone who may be interested:

It's fairly simple, instead of using the socket object, what I've done is use wget, which is a command prompt tool that can download files via command prompt, since you can execute commands through extendscript with system.callSystem, you can execute the wget tool to download the file from the url, all you need to do is download the wget tool from here: GNU Wget 1.19.4 for Windows then place wget.exe into the folder you want to download images to

I've written a snippet that works with After Effects that will download the image with wget, then import that file into After Effects, just change the paths to suit:

var folderLocation = "d:/wget";

var imageURL = "https://i.imgur.com/pKY6r1K.jpg";

var myCommand = "cd /d " + folderLocation + " & wget " + imageURL;

system.callSystem("cmd /c \"" + myCommand + "\"");

var path = new File(folderLocation + "/" + (imageURL.split("/")[imageURL.split("/").length - 1]));

if (path.exists) {

  var io = new ImportOptions(path);

  var videoFile = app.project.importFile(io);

} else {

  alert("There was a problem importing the image");

}

Participant
March 21, 2018

stevelewisza​ – thanks for posting! This brings me a little closer to a solution to a variation on this technique I've been trying to wrap my head around, which is pulling items from an Adobe Shared Library into AE. I think because the libraries are cloud based, each asset should have a web address, which could be called using your snippet. (Since I can't figure out a way to access Adobe Libraries directly inside of AE).

Many of the end users of my projects will use Macs, however. Do you know of a similar software that would be compatible with Mac? I suppose it wouldn't be too difficult to version the  script out for each OS. If there is something similar, would the process of calling the .dng via system.callSystem work the same way it does to call an .exe in Windows?

Any insight would be greatly appreciated!

stib
Inspiring
March 29, 2018

curl is probably your best bet on macs, (wget is available, but curl is built-in).

Inspiring
October 31, 2012

Just looked in the adobe javascript documentation and you should not put the http:// and you should put the http port number 80 at the end. So the connection should be "distilleryimage3.s3.amazonaws.com:80".

At the GET, make sure you have slashes before both "n".

videocpAuthor
Known Participant
October 31, 2012

Thanks, that was most helpful. At least I'm getting a response from the server. Working on the idea of image buckets and such. Some trial and error and more reading I was able to get this socket response; This was response from

webConnect.write('GET /distilleryimage5/17bdfa64237411e2a23c22000a1f9d66_7.jpg HTTP/1.0\n\n');

How do I save the results into an object that curItem.replace(new File( <Socket response> ); will work with? I did change the GET to 'binary' insteaed of HTTP:/1.0\n\n but just don't know what to do with it next.

You help and patience with my questions has been greatly appreciated. Learning curves and patience are not easily to over come or use at time. Thanks again.

Inspiring
October 31, 2012

Now you need to parse that response.

Here to get you started :

var response =  webConnect.readln();

var statusCode = new RegExp(/HTTP\/[0-9.]+\s([0-9]+)/g).exec(q)[1];

if (parseInt(statusCode) != 200) webConnect.close();

else{

    response =  webConnect.readln();

    /*read all the necessary lines;*/

    webConnect.close();

}

I suppose there is an undefined number of line in that header. So you'll read lines by lines and look for content that you want. Eventually you'll need to know the content-length that you'll feed to the webConnect.read(contentLengthValue). You might do it using regexp like i did for the first line (http://gskinner.com/RegExr/). You should look at the definition of a http response for how to finish processing this header, but it looks like there is an empty line before feeding the data.