Stream video to file, how to set binary option ( node js ).

1,211 views
Skip to first unread message

JLM

unread,
Jan 3, 2017, 7:24:34 AM1/3/17
to Haxe
I have an electron app that gets some videos from the web and stores them local so that they run instantly when the main app runs, so that the videos can be updated externally, but the video is garbled.

I have tried this code:

var file = js.node.Fs.createWriteStream('savedVideo.mp4');
var cb = function(){ screenLog( 'videoSaved'); };
file.end( cb );
var url = 'http://somewhereonweb.com/video.mp4';
var request = Http.get( url, function(response) {
          response.pipe( file );
}
( not sure how I atttach an error ).

This saves a file locally but it's probably saved as utf8 so it is no longer a video.

I found that there is a binary option in an older node js lib.

https://github.com/dionjwa/nodejs-std/blob/master/haxe/HttpEncoding.hx

but I am not sure if I should resort to that instead the haxe folder seems to be missing in the official repository.
https://github.com/HaxeFoundation/hxnodejs/tree/master/src

Seems the normal Haxe approach would be something like
var http = new haxe.Http( url );
but no way to set binary and in the onData I can't set the pipe.

I am using nightly Haxe, I could switch to neko for just the streaming videos to a file if it's simpler, not sure how to proceed.

JLM

unread,
Jan 3, 2017, 9:02:54 AM1/3/17
to Haxe
I tried with nodejs-std and in both cases the file created seems to be empty.

chunbiao huang

unread,
Jan 3, 2017, 9:31:30 PM1/3/17
to haxelang

2017-01-03 22:02 GMT+08:00 JLM <j...@justinfront.net>:
I tried with nodejs-std and in both cases the file created seems to be empty.

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

JLM

unread,
Jan 4, 2017, 4:43:26 AM1/4/17
to Haxe
Paling thank you for the link, not too sure on approach to creating externs, but it's not clear that this is set up to download video from external server, it looks like it's working with a local file there seems to be no Http or is that done externally? 

If I ported this to use 'Fs' using official hxnodejs then the main problem is that the api does not seem to allow access to stuff required, that is to say that any solution found on the web from the node community is not simple to translate to hxnodejs.

Also not sure on how to setup threading to allow several files to be grabbed at the same time. 

One thing I don't really understand looking at the code is that surely a video can be treated like a binary file for my use case so why is all the code needed to get the file type etc... surely if I need to chunk it then that can be done by treating it as a generic binary stream - why does the code need to care about the file type.  Sorry if this is nieve question.

But thank you for the link.  At moment I have a rough neko sync solution that does not care that the files are video, which works as a test, but for multiple videos it's quite slow and gives no feedback of progress, I am wondering if doing it in a language that allows threads like java but supports more standard haxe api might be simpler, also Java target code seems to expose itself properly so I can use java examples online as guide?- whereas the nodejshx seems to change and hide stuff in a way that seem not to make it more haxe standard or to give native access, but this is just my impression after looking at library and the examples.

Will threads and async allow the files to be downloaded faster?

Russ

unread,
Jan 4, 2017, 6:17:32 AM1/4/17
to Haxe
You're ending the write steam before the read stream finishes.  This seems to work:

 var file = js.node.Fs.createWriteStream('savedVideo.mp4');

 
var cb = function(){ trace( 'videoSaved'); };
 
 
var url = 'http://www.sample-videos.com/video/mp4/480/big_buck_bunny_480p_1mb.mp4';
 
var request = js.node.Http.get( url, function(response) {
   response
.pipe( file ).on('finish', function() { file.end(cb); });
 
});






JLM

unread,
Jan 4, 2017, 7:57:53 AM1/4/17
to Haxe
Thank you Russ that works :)  Silly me.
Reply all
Reply to author
Forward
0 new messages