use more OxJS, remove unused code

This commit is contained in:
j 2016-02-29 17:26:25 +05:30
parent e476d2d408
commit 402eae8068
6 changed files with 148 additions and 269 deletions

View file

@ -5,10 +5,6 @@
text-align: center; text-align: center;
} }
#additionalFeatures {
display: none;
}
#wrapper { #wrapper {
margin-top: 30px; margin-top: 30px;
width: 100%; width: 100%;

View file

@ -123,13 +123,6 @@ A transcribing and subtitling tool for Firefox. It is used for transcribing vide
</div> </div>
<div id="txtWrapper"> <div id="txtWrapper">
<textarea id="txt" class="OxThemeClassic"></textarea><br /> <br /> <textarea id="txt" class="OxThemeClassic"></textarea><br /> <br />
<div id="additionalFeatures">
<div id="addTimeWrap">
Time to add: <input id="timeToAdd" /><br />
Start no: <input id="startNo" /> <button id="addTime">Add</button><br />
<textarea id="addTimeResult"></textarea>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>

View file

@ -151,9 +151,10 @@ Ox.load({
videoHash = undefined; videoHash = undefined;
textArea.elem.val(''); textArea.elem.val('');
var path = data.files[0].name; var path = data.files[0].name;
if ($.inArray(getFileNameExt(path.toLowerCase()), [ var extension = Ox.last(path.toLowerCase().split('.'));
if (Ox.contains([
'oga', 'ogg', 'ogv', 'ogx', 'wav', 'webm', 'mp4', 'm4v' 'oga', 'ogg', 'ogv', 'ogx', 'wav', 'webm', 'mp4', 'm4v'
]) != -1) { ], extension)) {
loadVideo(data.files[0]); loadVideo(data.files[0]);
} else { } else {
alert('You can only open videos in formats supported by the browser.'); alert('You can only open videos in formats supported by the browser.');
@ -255,7 +256,6 @@ function loadTxt() {
} }
function saveTxt() { function saveTxt() {
var content = textArea.toSrt();
var txt = textArea.elem.val(); var txt = textArea.elem.val();
//console.log('saveTxt', videoHash, txt.length); //console.log('saveTxt', videoHash, txt.length);
globalUser.set_txt(videoHash, txt); globalUser.set_txt(videoHash, txt);

View file

@ -1,6 +1,3 @@
var url = "https://speedtrans.pad.ma";
//elem = string (elementId to make TextArea) //elem = string (elementId to make TextArea)
var TextArea = function(elem) { var TextArea = function(elem) {
this.elem = $('#' + elem); this.elem = $('#' + elem);
@ -39,7 +36,7 @@ TextArea.prototype.isTc = function() {
var pos = eDom.selectionStart; var pos = eDom.selectionStart;
var word = this.getWord(pos); var word = this.getWord(pos);
if (isValidTimecode(word)) { if (isValidTimecode(word)) {
return npt2ms(word); return Ox.parseDuration(word);
} else { } else {
return false; return false;
} }
@ -54,7 +51,7 @@ TextArea.prototype.insertTc = function(tcNpt) {
var val = this.elem.val(); var val = this.elem.val();
var pos = eDom.selectionStart; var pos = eDom.selectionStart;
if(!tcNpt) { if(!tcNpt) {
tcNpt = ms2npt(Video.get()); tcNpt = Ox.formatDuration(Video.get(), 3);
} }
var newVal = val.substring(0,pos) + "\n" + tcNpt + "\n" + val.substring(pos, val.length); var newVal = val.substring(0,pos) + "\n" + tcNpt + "\n" + val.substring(pos, val.length);
e.val(newVal); e.val(newVal);
@ -103,52 +100,29 @@ function cleanNewlines(str) {
TextArea.prototype.fromSrt = function(txt) { TextArea.prototype.fromSrt = function(txt) {
var that = this; var that = this;
this.spans = []; this.spans = [];
var cleaned = cleanNewlines(txt); var srt = Ox.parseSRT(txt);
var srt = strip(cleaned); Ox.print(srt);
var srt_ = srt.split('\n\n'); srt.forEach(function(s) {
var s; that.spans.push(new Span(s['in'], s.out, s.text, that.spans.length));
for(s in srt_) { });
st = srt_[s].split('\n');
if(st.length >=2) {
var n = st[0];
var i = strip(st[1].split(' --> ')[0]);
var o = strip(st[1].split(' --> ')[1]);
var t = st[2];
if(st.length > 2) {
for(j=3; j<st.length;j++) {
t += '\n'+st[j];
}
}
var is = toSeconds(i);
var os = toSeconds(o);
if (parseInt(n) - this.spans.length == 1) {
this.spans[this.spans.length] = new Span(is, os, t, that.spans.length);
} else {
return false;
}
}
}
var out = ''; var out = '';
var spans = this.spans; var spans = this.spans;
for (span in spans) { spans.forEach(function(sp, span) {
if (spans.hasOwnProperty(span)) { out += Ox.formatDuration(sp.tcInMs, 3) + "\n";
var sp = spans[span];
out += ms2npt(sp.tcInMs) + "\n";
out += sp.text; out += sp.text;
out += "\n"; out += "\n";
//If the outpoint of current span is equal to inpoint of next span, dont print out timecode, and just add the extra \n to go to next span. //If the outpoint of current span is equal to inpoint of next span, dont print out timecode, and just add the extra \n to go to next span.
if (span < spans.length - 1) { if (span < spans.length - 1) {
var p = parseInt(span) + 1; var p = parseInt(span) + 1;
if (spans[p].tcInMs != sp.tcOutMs) { if (spans[p].tcInMs != sp.tcOutMs) {
out += ms2npt(sp.tcOutMs) + "\n\n"; out += Ox.formatDuration(sp.tcOutMs, 3) + "\n\n";
} else { } else {
out += "\n"; out += "\n";
} }
} else if (parseInt(sp.tcOutMs) < Video.player.duration) { } else if (parseInt(sp.tcOutMs) < Video.player.duration) {
out += "\n" + ms2npt(sp.tcOutMs) + "\n\n"; out += "\n" + Ox.formatDuration(sp.tcOutMs, 3) + "\n\n";
}
}
} }
});
this.elem.val(out); this.elem.val(out);
if (this.elem.val == '' || this.spans.length == 0) { if (this.elem.val == '' || this.spans.length == 0) {
return false; return false;
@ -161,25 +135,22 @@ function strip(s) {
return s.replace(/^\s+|\s+$/g,""); return s.replace(/^\s+|\s+$/g,"");
} }
function toSeconds(t) { function formatEnc(data) {
var s = 0.0; var r = '', n = 1;
if(t) { data.forEach(function(item) {
var p = t.split(':'); r += n + ' ' + formatFrames(item['in']) + ' ' + formatFrames(item.out) + ' ' + item.text;
for(i=0;i<p.length;i++) { r += '\n\n';
s = s * 60 + parseFloat(p[i].replace(',', '.')) n++;
} });
} return r;
return s;
} }
function spansToSrt(arr, fmt, start_no) { function spansToSrt(arr, fmt, start_no) {
if (typeof start_no == 'undefined') { if (typeof start_no == 'undefined') {
start_no = 1; start_no = 1;
} }
var srt = ''; var srt = [];
var srtNo = start_no; arr.forEach(function(s) {
for (var k=0; k < arr.length; k++) {
var s = arr[k];
if (s.text.trim() == '') { if (s.text.trim() == '') {
} else { } else {
var text = s.text.trim(); var text = s.text.trim();
@ -188,20 +159,17 @@ function spansToSrt(arr, fmt, start_no) {
if (!s.tcOutMs) { if (!s.tcOutMs) {
s.tcOutMs = Video.player.duration; s.tcOutMs = Video.player.duration;
} }
srt.push({
'in': s.tcInMs,
out: s.tcOutMs,
text: text
});
}
});
if (fmt == 'srt') { if (fmt == 'srt') {
srt += srtNo + " "; return Ox.formatSRT(srt);
srt += "\n"; } else if (fmt == 'enc') {
srt += "0" + ms2npt(s.tcInMs).replace(".", ",") + " --> " + "0" + ms2npt(s.tcOutMs).replace(".", ","); return formatEnc(srt);
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; return srt;
} }
@ -241,24 +209,7 @@ TextArea.prototype.toSrt = function(fmt) {
this.spans = spans; this.spans = spans;
var srt = spansToSrt(spans, fmt); var srt = spansToSrt(spans, fmt);
//console.log(srt); //console.log(srt);
return Ox.encodeUTF8(srt); return srt;
}
TextArea.prototype.addTime = function(ms, start_no) {
//console.log(ms);
this.toSrt();
var s = [];
var spans = this.spans;
for (var i=0; i < spans.length;i++) {
s[i] = {
index: i,
tcOutMs: spans[i].tcOutMs + ms,
text: spans[i].text,
tcInMs: spans[i].tcInMs + ms
}
}
return spansToSrt(s, 'srt', start_no);
} }
@ -270,7 +221,7 @@ TextArea.prototype.toHTML = function() {
span = spans[i]; span = spans[i];
txt = ''; txt = '';
txt += '<p>\n'; txt += '<p>\n';
txt += '<a href="https://pad.ma/PADMA_VIDEO_ID/' + ms2npt(span.tcInMs) + "-" + ms2npt(span.tcOutMs) + '">'; txt += '<a href="https://pad.ma/PADMA_VIDEO_ID/' + Ox.formatDuration(span.tcInMs, 3) + "-" + Ox.formatDuration(span.tcOutMs, 3) + '">';
txt += (i + 1).toString(); txt += (i + 1).toString();
txt += "</a>\n"; txt += "</a>\n";
txt += '<br />\n'; txt += '<br />\n';
@ -285,9 +236,9 @@ TextArea.prototype.toHTML = function() {
//creates new Span (tcIn and tcOut in npt format) //creates new Span (tcIn and tcOut in npt format)
var Span = function(tcIn, tcOut, text, index) { var Span = function(tcIn, tcOut, text, index) {
this.index = index; this.index = index;
this.tcOutMs = npt2ms(tcOut); this.tcOutMs = Ox.isString(tcOut) ? Ox.parseDuration(tcOut) : tcOut;
this.text = text; this.text = text;
this.tcInMs = npt2ms(tcIn); this.tcInMs = Ox.isString(tcOut) ? Ox.parseDuration(tcIn) : tcIn;
} }
var SeekBar = function(elem) { var SeekBar = function(elem) {
@ -307,7 +258,7 @@ SeekBar.prototype.init = function() {
drag: function() { drag: function() {
clearInterval(videoListener); clearInterval(videoListener);
var pos = that.get(); var pos = that.get();
$('#timeCode').html(ms2npt(pos)); $('#timeCode').html(Ox.formatDuration(pos, 3));
}, },
stop: function() { stop: function() {
var pos = that.get(); var pos = that.get();
@ -432,7 +383,7 @@ function loadCuts(url, id) {
pandora_api('get', {id: id, keys: ['cuts']}, function(result) { pandora_api('get', {id: id, keys: ['cuts']}, function(result) {
var txt = ''; var txt = '';
result.data.cuts.forEach(function(cut) { result.data.cuts.forEach(function(cut) {
textArea.insertTc(ms2npt(cut)); textArea.insertTc(Ox.formatDuration(cut));
}); });
}); });
} }

View file

@ -121,7 +121,7 @@ VideoPlayer.prototype.togglePause = function() {
VideoPlayer.prototype.listener = function() { VideoPlayer.prototype.listener = function() {
var t = Video.get(); var t = Video.get();
var npt = ms2npt(t); var npt = Ox.formatDuration(t, 3);
$('#timeCode').html(npt); $('#timeCode').html(npt);
var seekBarPos = parseInt((t/Video.duration) * 320); var seekBarPos = parseInt((t/Video.duration) * 320);
$('#seekPointer').css("left", seekBarPos + "px"); $('#seekPointer').css("left", seekBarPos + "px");

View file

@ -1,80 +1,19 @@
function npt2ms(npt) { function formatFrames(seconds) {
var ms = 0.0 var npt = Ox.formatDuration(seconds, 3);
npt = String(npt);
var p = npt.split(':')
for(i=0;i<p.length;i++) {
ms = ms * 60 + parseFloat(p[i])
}
return ms;
}
function ms2npt(seconds) {
var ms = seconds * 1000;
var it, ss, mm, hh, npt;
var it = parseInt(ms / 1000)
ms = ms - it * 1000;
if (ms.toString().length > 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 dotpos = npt.lastIndexOf(".");
var mmStr = npt.substring(dotpos + 1, npt.length); var mmStr = npt.substring(dotpos + 1, npt.length);
var mmInt = parseInt(mmStr); var mmInt = parseInt(mmStr);
if (fmt == 'PAL') {
var frames = parseInt((mmInt / 1000) * 24); var frames = parseInt((mmInt / 1000) * 24);
} else if (fmt == "NTSC") {
var frames = parseInt((mmInt / 1000) * 29.97);
}
var framesTc = ''; var framesTc = '';
var joinToken = ":"; var joinToken = ":";
var framesTc = npt.substring(0, dotpos ) + joinToken + frames; var framesTc = npt.substring(0, dotpos ) + joinToken + frames;
return framesTc; return framesTc;
} }
function ms2time(ms) {
var npt = ms2npt(ms)
return npt.substr(npt.length-9, npt.length-6);
}
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) { function isValidTimecode(tc) {
var tc = $.trim(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]?$"); 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 return nptRegex.test(tc);
}
//where filters is a JSON object, for eg. {'Video Files': '*.dv;*.ogg;*.ogv;*.ogx;*.avi;*.mov;*.mp4;*.mpeg;*.mpg;*.vob'}
function selectFile(filters, callback) {
alert('select file');
//fixme. select file
callback && callback(fp);
}
function getFileNameExt(filename) {
var dotPos = filename.lastIndexOf(".");
var ext = filename.substring(dotPos + 1, filename.length);
return ext;
} }
function getFileNameSansExt(filename) { function getFileNameSansExt(filename) {