<commit_before>var urlValue,
  re = /=(.+)/,
  videoID = '',
  tag,
  firstScriptTag,
  player,
  rates,
  currentRate,
  rateIndex,
  popcornVideo,
  subtitles,
  sub,
  subtitleChangeInterval,
  timeStart,
  timeEnd,
  prevSub,
  nextSub,
  editSub,
  videoData,
  currentSubIndex,
  startCaption;

function onYouTubeIframeAPIReady() {
  player = new YT.Player('player', {
    height: '340',
    width: '560',
    videoId: videoID,
    playerVars: {
      'cc_load_policy': 1,
      'controls': 0
    },
    events: {
      'onReady': onPlayerReady,
    }
  });
}

function onPlayerReady(event) {
  rates = player.getAvailablePlaybackRates();
  currentRate = player.getPlaybackRate();
  rateIndex = rates.indexOf(currentRate);
  event.target.playVideo();
  subtitleChangeInterval = setInterval(subtitleRefresh, 100);
  
  $('.sub-edit').focus(function() {
    player.pauseVideo();
  });
}

function pauseVideo() {
  if (player.getPlayerState() === 1) {
    player.pauseVideo();
  }
  else {
    player.playVideo();
  }
}

function slowdownVideo() {
  if (rateIndex > 0) {
    player.setPlaybackRate(rates[rateIndex-1]);
    rateIndex -= 1;
  }
}

function speedupVideo() {
  if (rateIndex < rates.length -1) {
    player.setPlaybackRate(rates[rateIndex+1]);
    rateIndex += 1;
  }
}

function rewindVideo() {
  player.seekTo((player.getCurrentTime()-2), true);
  player.playVideo();
  console.log(player.getCurrentTime());
}

function forwardVideo() {
  player.seekTo((player.getCurrentTime()+2), true);
  player.playVideo();
  console.log(player.getCurrentTime());
}

Mousetrap.bind('ctrl+1', pauseVideo);
Mousetrap.bind('ctrl+4', slowdownVideo);
Mousetrap.bind('ctrl+5', speedupVideo);
Mousetrap.bind('ctrl+2', rewindVideo);
Mousetrap.bind('ctrl+3', forwardVideo);
Mousetrap.bind('ctrl+shift+s', saveCaption);
Mousetrap.bind('ctrl+shift+a', addCaption);
Mousetrap.bind('ctrl+shift+d', deleteCaption);


function subtitleRefresh() {
  console.log(player.getCurrentTime());
  $('#player-time').text(player.getCurrentTime());

  if (player.getPlayerState() === 1) {
    if (player.getCurrentTime() > (subtitles[subtitles.length-1].start + subtitles[subtitles.length-1].dur)) {
      $('.sub-edit').val('');
      $('.edit-start').val('');
      $('.edit-end').val('');
      $('.sub-next').val('');
      $('.next-start').val('');
      $('.next-end').val('');
      $('.sub-prev').val(subtitles[subtitles.length-1].value);
      $('.prev-start').val(subtitles[subtitles.length-1].start);
      $('.prev-end').val(subtitles[subtitles.length-1].start + subtitles[subtitles.length-1].dur);
      $('#subtitles h3').text('');
      return false;
    }

    for (sub in subtitles) {
      timeStart = subtitles[sub].start;
      timeEnd = subtitles[sub].start + subtitles[sub].dur;
      // if (player.getCurrentTime() < subtitles[0].start) {
      //   currentSubIndex = sub;
      //   editSub = subtitles[0];
      //   nextSub = subtitles[1];
      //   $('.sub-edit').val('');
      //   $('.edit-start').val('');
      //   $('.edit-end').val('');
      //   $('.sub-next').val('');
      //   $('.next-start').val('');
      //   $('.next-end').val('');
      //   $('.sub-prev').val('');
      //   $('.prev-start').val('');
      //   $('.prev-end').val('');
      //   return false;
      // }

      if (player.getCurrentTime() >= timeStart && player.getCurrentTime() <= timeEnd) {
        currentSubIndex = sub;
        editSub = subtitles[sub];
        prevSub = subtitles[parseInt(sub)-1];
        nextSub = subtitles[parseInt(sub)+1];
        console.log(currentSubIndex);
        if (nextSub && prevSub) {
          $('#subtitles h3').text(editSub.value);
          $('.sub-edit').val(editSub.value);
          $('.edit-start').val(editSub.start);
          $('.edit-end').val(editSub.start + editSub.dur);
          $('.sub-next').val(nextSub.value);
          $('.next-start').val(nextSub.start);
          $('.next-end').val(nextSub.start + nextSub.dur);
          $('.sub-prev').val(prevSub.value);
          $('.prev-start').val(prevSub.start);
          $('.prev-end').val(prevSub.start + prevSub.dur);
          return false;
        }
        else if (!prevSub && !nextSub) {
          $('#subtitles h3').text(editSub.value);
          $('.sub-edit').val(editSub.value);
          $('.edit-start').val(editSub.start);
          $('.edit-end').val(editSub.start + editSub.dur);
          $('.sub-next').val('');
          $('.next-start').val('');
          $('.next-end').val('');
          $('.sub-prev').val('');
          $('.prev-start').val('');
          $('.prev-end').val('');
          return false;
        }
        else if (!prevSub) {
          $('#subtitles h3').text(editSub.value);
          $('.sub-edit').val(editSub.value);
          $('.edit-start').val(editSub.start);
          $('.edit-end').val(editSub.start + editSub.dur);
          $('.sub-next').val(nextSub.value);
          $('.next-start').val(nextSub.start);
          $('.next-end').val(nextSub.start + nextSub.dur);
          $('.sub-prev').val('');
          $('.prev-start').val('');
          $('.prev-end').val('');
          return false;
        }
        else {
          $('#subtitles h3').text(editSub.value);
          $('.sub-edit').val(editSub.value);
          $('.edit-start').val(editSub.start);
          $('.edit-end').val(editSub.start + editSub.dur);
          $('.sub-next').val('');
          $('.next-start').val('');
          $('.next-end').val('');
          $('.sub-prev').val(prevSub.value);
          $('.prev-start').val(prevSub.start);
          $('.prev-end').val(prevSub.start + prevSub.dur);
          return false;          
        }
      }
    }
  }
}

function deleteCaption() {
  videoData.captions.splice(currentSubIndex, 1);
  $.ajax({
    url: '/video/' + videoID,
    type: 'POST',
    data: JSON.stringify(videoData),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json'
  });
  subtitles = videoData.captions;
  $('#subtitles h3').text('');
  $('.sub-edit').val('');
  $('.edit-start').val('');
  $('.edit-end').val('');
  rewindVideo();
  player.playVideo();
}

function addCaption() {
  console.log(videoData);
  pauseVideo();
  if (currentSubIndex) {
    startCaption = 
    {
      'start': player.getCurrentTime(),
      'dur': 5.0,
      'value': 'Add caption text and change beginning and ending times',
      'extra_data': []
    };
    videoData.captions.splice(currentSubIndex, 0, startCaption);
    subtitles = videoData.captions;
    editSub = subtitles[currentSubIndex];
    prevSub = subtitles[parseInt(currentSubIndex)-1];
    nextSub = subtitles[parseInt(currentSubIndex)+1];
    $('#subtitles h3').text(editSub.value);
    $('.sub-edit').val(editSub.value);
    $('.edit-start').val(editSub.start);
    $('.edit-end').val(editSub.start + editSub.dur);
    $('.sub-next').val(nextSub.value);
    $('.next-start').val(nextSub.start);
    $('.next-end').val(nextSub.start + nextSub.dur);
    $('.sub-prev').val(prevSub.value);
    $('.prev-start').val(prevSub.start);
    $('.prev-end').val(prevSub.start + prevSub.dur);
    saveCaption();
  }
  else {
    currentSubIndex = 0;
    startCaption = 
    {
      'start': player.getCurrentTime(),
      'dur': 5.0,
      'value': 'Add caption text and change beginning and ending times', 
      'extra_data': []
    };
    videoData.captions.push(startCaption);
    subtitles = videoData.captions;
    console.log(subtitles);
    editSub = subtitles[currentSubIndex];
    console.log(editSub);
    console.log(editSub.value)
    $('#subtitles h3').text(editSub.value);
    $('.sub-edit').val(editSub.value);
    $('.edit-start').val(editSub.start);
    $('.edit-end').val(editSub.start + editSub.dur);
    saveCaption();
  }
}

function saveCaption() {
  console.log(currentSubIndex);
  videoData.captions[currentSubIndex].value = $('.sub-edit').val();
  videoData.captions[currentSubIndex].start = parseFloat($('.edit-start').val());
  videoData.captions[currentSubIndex].dur = parseFloat($('.edit-end').val()) - parseFloat($('.edit-start').val());
  if (currentSubIndex > 0) {
    videoData.captions[parseInt(currentSubIndex)-1].start = parseFloat($('.prev-start').val());
    videoData.captions[parseInt(currentSubIndex)-1].dur = parseFloat($('.prev-end').val()) - parseFloat($('.prev-start').val());    
  }<commit_msg>Fix pause on add captions and save is not automatically called in add caption<commit_after>var urlValue,
  re = /=(.+)/,
  videoID = '',
  tag,
  firstScriptTag,
  player,
  rates,
  currentRate,
  rateIndex,
  popcornVideo,
  subtitles,
  sub,
  subtitleChangeInterval,
  timeStart,
  timeEnd,
  prevSub,
  nextSub,
  editSub,
  videoData,
  currentSubIndex,
  startCaption;

function onYouTubeIframeAPIReady() {
  player = new YT.Player('player', {
    height: '340',
    width: '560',
    videoId: videoID,
    playerVars: {
      'cc_load_policy': 1,
      'controls': 0
    },
    events: {
      'onReady': onPlayerReady,
    }
  });
}

function onPlayerReady(event) {
  rates = player.getAvailablePlaybackRates();
  currentRate = player.getPlaybackRate();
  rateIndex = rates.indexOf(currentRate);
  event.target.playVideo();
  subtitleChangeInterval = setInterval(subtitleRefresh, 100);
  
  $('.sub-edit').focus(function() {
    player.pauseVideo();
  });
}

function pauseVideo() {
  if (player.getPlayerState() === 1) {
    player.pauseVideo();
  }
  else {
    player.playVideo();
  }
}

function slowdownVideo() {
  if (rateIndex > 0) {
    player.setPlaybackRate(rates[rateIndex-1]);
    rateIndex -= 1;
  }
}

function speedupVideo() {
  if (rateIndex < rates.length -1) {
    player.setPlaybackRate(rates[rateIndex+1]);
    rateIndex += 1;
  }
}

function rewindVideo() {
  player.seekTo((player.getCurrentTime()-2), true);
  player.playVideo();
  console.log(player.getCurrentTime());
}

function forwardVideo() {
  player.seekTo((player.getCurrentTime()+2), true);
  player.playVideo();
  console.log(player.getCurrentTime());
}

Mousetrap.bind('ctrl+1', pauseVideo);
Mousetrap.bind('ctrl+4', slowdownVideo);
Mousetrap.bind('ctrl+5', speedupVideo);
Mousetrap.bind('ctrl+2', rewindVideo);
Mousetrap.bind('ctrl+3', forwardVideo);
Mousetrap.bind('ctrl+shift+s', saveCaption);
Mousetrap.bind('ctrl+shift+a', addCaption);
Mousetrap.bind('ctrl+shift+d', deleteCaption);


function subtitleRefresh() {
  console.log(player.getCurrentTime());
  $('#player-time').text(player.getCurrentTime());

  if (player.getPlayerState() === 1) {
    if (player.getCurrentTime() > (subtitles[subtitles.length-1].start + subtitles[subtitles.length-1].dur)) {
      $('.sub-edit').val('');
      $('.edit-start').val('');
      $('.edit-end').val('');
      $('.sub-next').val('');
      $('.next-start').val('');
      $('.next-end').val('');
      $('.sub-prev').val(subtitles[subtitles.length-1].value);
      $('.prev-start').val(subtitles[subtitles.length-1].start);
      $('.prev-end').val(subtitles[subtitles.length-1].start + subtitles[subtitles.length-1].dur);
      $('#subtitles h3').text('');
      return false;
    }

    for (sub in subtitles) {
      timeStart = subtitles[sub].start;
      timeEnd = subtitles[sub].start + subtitles[sub].dur;
      // if (player.getCurrentTime() < subtitles[0].start) {
      //   currentSubIndex = sub;
      //   editSub = subtitles[0];
      //   nextSub = subtitles[1];
      //   $('.sub-edit').val('');
      //   $('.edit-start').val('');
      //   $('.edit-end').val('');
      //   $('.sub-next').val('');
      //   $('.next-start').val('');
      //   $('.next-end').val('');
      //   $('.sub-prev').val('');
      //   $('.prev-start').val('');
      //   $('.prev-end').val('');
      //   return false;
      // }

      if (player.getCurrentTime() >= timeStart && player.getCurrentTime() <= timeEnd) {
        currentSubIndex = sub;
        editSub = subtitles[sub];
        prevSub = subtitles[parseInt(sub)-1];
        nextSub = subtitles[parseInt(sub)+1];
        console.log(currentSubIndex);
        if (nextSub && prevSub) {
          $('#subtitles h3').text(editSub.value);
          $('.sub-edit').val(editSub.value);
          $('.edit-start').val(editSub.start);
          $('.edit-end').val(editSub.start + editSub.dur);
          $('.sub-next').val(nextSub.value);
          $('.next-start').val(nextSub.start);
          $('.next-end').val(nextSub.start + nextSub.dur);
          $('.sub-prev').val(prevSub.value);
          $('.prev-start').val(prevSub.start);
          $('.prev-end').val(prevSub.start + prevSub.dur);
          return false;
        }
        else if (!prevSub && !nextSub) {
          $('#subtitles h3').text(editSub.value);
          $('.sub-edit').val(editSub.value);
          $('.edit-start').val(editSub.start);
          $('.edit-end').val(editSub.start + editSub.dur);
          $('.sub-next').val('');
          $('.next-start').val('');
          $('.next-end').val('');
          $('.sub-prev').val('');
          $('.prev-start').val('');
          $('.prev-end').val('');
          return false;
        }
        else if (!prevSub) {
          $('#subtitles h3').text(editSub.value);
          $('.sub-edit').val(editSub.value);
          $('.edit-start').val(editSub.start);
          $('.edit-end').val(editSub.start + editSub.dur);
          $('.sub-next').val(nextSub.value);
          $('.next-start').val(nextSub.start);
          $('.next-end').val(nextSub.start + nextSub.dur);
          $('.sub-prev').val('');
          $('.prev-start').val('');
          $('.prev-end').val('');
          return false;
        }
        else {
          $('#subtitles h3').text(editSub.value);
          $('.sub-edit').val(editSub.value);
          $('.edit-start').val(editSub.start);
          $('.edit-end').val(editSub.start + editSub.dur);
          $('.sub-next').val('');
          $('.next-start').val('');
          $('.next-end').val('');
          $('.sub-prev').val(prevSub.value);
          $('.prev-start').val(prevSub.start);
          $('.prev-end').val(prevSub.start + prevSub.dur);
          return false;          
        }
      }
    }
  }
}

function deleteCaption() {
  videoData.captions.splice(currentSubIndex, 1);
  $.ajax({
    url: '/video/' + videoID,
    type: 'POST',
    data: JSON.stringify(videoData),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json'
  });
  subtitles = videoData.captions;
  $('#subtitles h3').text('');
  $('.sub-edit').val('');
  $('.edit-start').val('');
  $('.edit-end').val('');
  rewindVideo();
  player.playVideo();
}

function addCaption() {
  console.log(videoData);
  pauseVideo();
  if (currentSubIndex) {
    startCaption = 
    {
      'start': player.getCurrentTime(),
      'dur': 5.0,
      'value': 'Add caption text and change beginning and ending times',
      'extra_data': []
    };
    videoData.captions.splice(currentSubIndex, 0, startCaption);
    subtitles = videoData.captions;
    editSub = subtitles[currentSubIndex];
    prevSub = subtitles[parseInt(currentSubIndex)-1];
    nextSub = subtitles[parseInt(currentSubIndex)+1];
    $('#subtitles h3').text(editSub.value);
    $('.sub-edit').val(editSub.value);
    $('.edit-start').val(editSub.start);
    $('.edit-end').val(editSub.start + editSub.dur);
    $('.sub-next').val(nextSub.value);
    $('.next-start').val(nextSub.start);
    $('.next-end').val(nextSub.start + nextSub.dur);
    $('.sub-prev').val(prevSub.value);
    $('.prev-start').val(prevSub.start);
    $('.prev-end').val(prevSub.start + prevSub.dur);
  }
  else {
    currentSubIndex = 0;
    startCaption = 
    {
      'start': player.getCurrentTime(),
      'dur': 5.0,
      'value': 'Add caption text and change beginning and ending times', 
      'extra_data': []
    };
    videoData.captions.push(startCaption);
    subtitles = videoData.captions;
    console.log(subtitles);
    editSub = subtitles[currentSubIndex];
    console.log(editSub);
    console.log(editSub.value)
    $('#subtitles h3').text(editSub.value);
    $('.sub-edit').val(editSub.value);
    $('.edit-start').val(editSub.start);
    $('.edit-end').val(editSub.start + editSub.dur);
  }
}

function saveCaption() {
  console.log(currentSubIndex);
  videoData.captions[currentSubIndex].value = $('.sub-edit').val();
  videoData.captions[currentSubIndex].start = parseFloat($('.edit-start').val());
  videoData.captions[currentSubIndex].dur = parseFloat($('.edit-end').val()) - parseFloat($('.edit-start').val());
  if (currentSubIndex > 0) {
    videoData.captions[parseInt(currentSubIndex)-1].start = parseFloat($('.prev-start').val());
    videoData.captions[parseInt(currentSubIndex)-1].dur = parseFloat($('.prev-end').val()) - parseFloat($('.prev-start').val());    
  }
{'fraction_non_alphanumeric': 0.2155831739961759, 'fraction_numerical': 0.006931166347992352, 'mean_word_length': 2.831730769230769, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3577515', 'n_tokens_mistral': 5772, 'n_tokens_neox': 5737, 'n_words': 757}