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;
}
#additionalFeatures {
display: none;
}
#wrapper {
margin-top: 30px;
width: 100%;

View file

@ -123,13 +123,6 @@ A transcribing and subtitling tool for Firefox. It is used for transcribing vide
</div>
<div id="txtWrapper">
<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>

View file

@ -151,9 +151,10 @@ Ox.load({
videoHash = undefined;
textArea.elem.val('');
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'
]) != -1) {
], extension)) {
loadVideo(data.files[0]);
} else {
alert('You can only open videos in formats supported by the browser.');
@ -255,7 +256,6 @@ function loadTxt() {
}
function saveTxt() {
var content = textArea.toSrt();
var txt = textArea.elem.val();
//console.log('saveTxt', videoHash, txt.length);
globalUser.set_txt(videoHash, txt);

View file

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

View file

@ -1,80 +1,19 @@
function npt2ms(npt) {
var ms = 0.0
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);
function formatFrames(seconds) {
var npt = Ox.formatDuration(seconds, 3);
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 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, 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;
return nptRegex.test(tc);
}
function getFileNameSansExt(filename) {