function npt2ms(npt) {
var ms = 0.0;
npt = String(npt);
var p = npt.split(':');
for(var i=0;i
3) {
ms = ms.toString().substring(0,3);
}
ss = it % 60;
mm = ((it - ss) / 60) % 60;
hh = ((it - (mm * 60) - ss) / 3600) % 60;
npt = hh+':'+strpad(mm.toString(), '0', 2, 'left')
npt += ':'+strpad(ss.toString(), '0', 2, 'left')
npt += '.'+strpad(ms.toString(), '0', 3, 'left')
return npt;
}
function ms2frames(ms, fmt) {
if (!fmt) var fmt = "PAL";
var npt = ms2npt(ms);
var dotpos = npt.lastIndexOf(".");
var mmStr = npt.substring(dotpos + 1, npt.length);
var mmInt = parseInt(mmStr);
if (fmt == 'PAL') {
var frames = parseInt((mmInt / 1000) * 24);
} else if (fmt == "NTSC") {
var frames = parseInt((mmInt / 1000) * 29.97);
}
var framesTc = '';
var joinToken = ":";
var framesTc = npt.substring(0, dotpos ) + joinToken + frames;
return framesTc;
}
function ms2time(ms) {
var npt = ms2npt(ms)
return npt.substr(npt.length-9, npt.length-6);
}
function framesToNpt(timeCode) {
var frames = timeCode.substring(9, 11);
var ms = parseInt(frames) / 25 * 1000;
var ms = String(ms);
var ms = strpad(ms, '0', 3, 'right');
var timeCodeNpt = timeCode.substring(0,8) + "." + ms;
return timeCodeNpt;
}
function strpad(str, pad, len, dir) {
while (str.length < len) {
if (dir == 'left')
str = pad + str;
else if (dir == 'right')
str = str + pad;
}
return str;
}
function isValidTimecode(tc) {
var tc = $.trim(tc);
var nptRegex = new RegExp("^[0-9][0-9]?\:[0-9][0-9]\:[0-9][0-9][\.|\,|\:][0-9]?[0-9]?[0-9]?$");
return nptRegex.test(tc);to
}
//where filters is a JSON object, for eg. {'Video Files': '*.dv;*.ogg;*.ogv;*.ogx;*.avi;*.mov;*.mp4;*.mpeg;*.mpg;*.vob'}
function selectFile(filters) {
// netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
fp.init(window, "Choose a File", nsIFilePicker.modeOpen);
//fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText);
// fp.appendFilter("Ogg Video Files", "*.ogg;*.ogv;*.ogx;");
for (var f in filters) {
if (filters.hasOwnProperty(f)) {
fp.appendFilter(f, filters[f]);
}
}
// fp.appendFilter("All Video Files", "*.dv;*.ogg;*.ogv;*.ogx;*.avi;*.mov;*.mp4;*.mpeg;*.mpg;*.vob");
fp.appendFilters(nsIFilePicker.filterAll);
var rv = fp.show();
if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
return fp;
/*
var file = fp.file;
var path = fp.file.path;
return path;
*/
}
return false;
}
function mozillaSaveFile(filePath,content)
{
if(window.Components) {
try {
// netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(filePath);
if(!file.exists())
file.create(0,0664);
var os = Components.classes["@mozilla.org/intl/converter-output-stream;1"].createInstance(Components.interfaces.nsIConverterOutputStream);
var out = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
out.init(file,0x20|0x02,00004,null);
os.init(out, "UTF-8", 0, 0x0000);
os.writeString(content);
//out.write(content,content.length);
os.close();
//out.flush();
out.close();
return true;
} catch(ex) {
alert(ex);
return false;
}
}
return null;
}
function mozillaSaveAs() {
const nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
fp.init(window, "Save srt as...", Components.interfaces.nsIFilePicker.modeSave);
fp.appendFilters(Components.interfaces.nsIFilePicker.filterAll);
var output = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
var pth = typeof(srtFilePath) != 'undefined' ? srtFilePath : filePath + ".srt";
output.initWithPath(pth);
fp.defaultString = output.leafName;
var rv = fp.show();
if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
return fp.file.path;
/*
var file = fp.file;
srtFilePath = file.path;
saveSrt(srtFilePath);
return true;
*/
}
return false;
}
// Returns null if it can't do it, false if there's an error, or a string of the content if successful
function mozillaLoadFile(filePath)
{
if(window.Components) {
try {
// netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(filePath);
if(!file.exists())
return null;
var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
inputStream.init(file,0x01,00004,null);
//var sInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
//sInputStream.init(inputStream);
var charset = "UTF-8";
const replacementChar = Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER;
var cInputStream = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
.createInstance(Components.interfaces.nsIConverterInputStream);
cInputStream.init(inputStream, charset, 1024, replacementChar);
var str = {};
var contents = '';
while (cInputStream.readString(4096, str) != 0) {
contents += str.value;
}
//var contents = sInputStream.read(sInputStream.available());
cInputStream.close();
//sInputStream.close();
inputStream.close();
return contents;
} catch(ex) {
alert(ex);
return false;
}
}
return null;
}
function checkFileExists(filePath) {
if(window.Components) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(filePath);
if (file.exists()) {
return true;
} else {
return false;
}
} catch(ex) {
//alert("Error");
return null;
}
}
}
function getFileNameExt(filename) {
var dotPos = filename.lastIndexOf(".");
var ext = filename.substring(dotPos + 1, filename.length);
return ext;
}
function getFileNameSansExt(filename) {
var dotPos = filename.lastIndexOf(".");
if (dotPos != '-1') {
var filenameSansExt = filename.substring(0,dotPos);
} else {
var filenameSansExt = filename;
}
return filenameSansExt;
}
function spansToSrt(duration, arr, fmt, start_no) {
if (typeof start_no == 'undefined') {
start_no = 1;
}
var srt = '';
var srtNo = start_no;
// Ox.print(arr, fmt);
if (fmt == 'layers') {
var layers = Ox.map(arr, function(v) {
// Ox.print(v);
if (!v.tcOutMs) {
v.tcOutMs = parseInt(duration * 1000);
}
return {
'in': v.tcInMs / 1000,
'out': v.tcOutMs / 1000,
'value': v.text,
'duration': (v.tcOutMs - v.tcInMs) / 1000,
'id': '_' + Ox.uid()
}
});
//console.log("layers", layers);
return layers;
}
for (var k=0; k < arr.length; k++) {
var s = arr[k];
if (s.text.trim() == '') {
} else {
var text = s.text.trim();
linebreaksRegex = new RegExp('\n+', "g")
text = text.replace(linebreaksRegex, "\n");
if (!s.tcOutMs) {
s.tcOutMs = parseInt(duration * 1000);
}
if (fmt == 'srt') {
srt += srtNo + " ";
srt += "\n";
srt += "0" + ms2npt(s.tcInMs).replace(".", ",") + " --> " + "0" + ms2npt(s.tcOutMs).replace(".", ",");
srt += "\n";
srt += text;
srt += "\n\n";
}
else if (fmt == 'enc') {
srt += srtNo + ms2frames(s.tcInMs) + " " + ms2frames(s.tcOutMs) + " " + text;
srt += "\n\n";
}
srtNo++;
}
}
return srt;
}
function cleanNewlines(str) {
var s = str.replace(/\r\n|\r|\n/g, '\n');
return s;
}