summaryrefslogtreecommitdiff
path: root/index.html
blob: 1f2c8d2c04a3ac9cacf6f0bd23458af2d989bc63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<html>
  <head>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">

	<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" media="screen"/>

	<title>YouTube Recutter</title>

	<style type="text/css">
	  td, th { padding: 0.5em; }
	  input.timebox { width: 5em; }
	  button { margin: 0.25em; }
	</style>
	<script type="text/javascript">
function onlyUnique(value, index, self) {
    return self.indexOf(value) === index;
}

// read a cutlist and turn it into a URL fragment string
// format: #ytid,ytid,ytid:i-e-s,i-e-s,i-e-s
// - ytid is a youtube video id
// - i is an index into the list of ytids
// - s is a start time in decimal seconds
// - e is an end time in decimal seconds
function frag_make(cutlist) {
	var vid_list = cutlist.map(function(x) {return x.id}).concat([video_id]).filter(onlyUnique);
	var cuts = cutlist.map(function(x) {
		return {id: vid_list.indexOf(x.id),
				start: x.start,
				end: x.end}});

	var frag = vid_list.join(",") + ":" +
		cuts.map(function(x) { return [x.id, x.start, x.end].join("-") }).join(",") + ":" +
		($("#repeat-p")[0].checked ? "r" : "");

	return frag;
}

// read a URL fragment string and turn it into a cutlist
function frag_parse(frag) {
	var vc
	vc = (frag + "::").split(":");
	var vids = vc[0].split(",");
	var cuts = vc[1].split(",");

	var flags = vc[2].split("");
	$("#repeat-p")[0].checked = (flags.indexOf("r") != -1);

	var cutlist = cuts.map(function(x) { ise = x.split("-");
										 return {id:vids[+ise[0]], start:+ise[1], end:+ise[2]}; })
	return cutlist;
}

	</script>
  </head>
  <body>
    <a href="https://github.com/chronomex/youtube-remixer"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/652c5b9acfaddf3a9c326fa6bde407b87f7be0f4/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6f72616e67655f6666373630302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"></a>
	<div class="container">
	<div class="page-header">
	  <h1>YouTube Recutter</h1>
	  <p class="lead">Create and share an edit of a YouTube video</p>
	</div>

	<div class="col-md-9">
	  <div id="player">
		<label>Paste link to video:
		  <input type="text" id="video-link"/>
		</label>
		<button id="load-button" disabled="true" onclick="load_video_button();">load</button>
	  </div>
	</div>

	<div class="col-md-3">
	  <button id="play-button" onclick="play_from_cutlist();">play this cut &gt;</button>
	  <hr/>
	  <button id="go-button" onclick="b_grab();">cut here!</button>
	  <button id="add-button" onclick="cutlist_append_empty();">add a blank cut</button>
	  <button id="share-button" onclick="b_share();">save to url</button>
	  <label><input type="checkbox" id="repeat-p"> repeat</label>

	  <hr/>
	  <table class="table" id="cutlist">
		<caption>Cut list (seconds)</caption>
		<thead>
		  <tr><th>from</th><th>to</th></tr>
		</thead>
		<tbody></tbody>
	  </table>
	  <p>copyright by chronomex, 2015. all rights reserved.</p>
	</div>

	</div>

	<script type="text/javascript">
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an iframe (and player)
//    after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
	$("#load-button")[0].disabled = false;
	if (video_id) {
		load_video();
	}
}

var video_id;
function load_video_button() {
	var video_url = $("#video-link")[0].value;
	video_id = video_url.match(/(?:youtu\.be\/|[?&]v=)([-_A-Za-z0-9]*)/)[1];
	load_video();
}
function load_video() {
    player = new YT.Player('player', {
        height: '390',
        width: '640',
        events: {
            'onReady': onPlayerReady,
        }
    });
}


// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
	player.cueVideoById(video_id);
}

// 5. The API calls this function when the player's state changes.
function onPlayerStateChange(event) {
}

function cutlist_append_empty() {
	var row = '<tr><td><input type="number" min="0" class="b-start timebox" name="start"/></td><td><input type="number" min="0" class="b-end timebox" name="end"/></td></tr>';

	$("#cutlist tbody").append(row);
	return $("#cutlist tbody tr").last()[0];
}

function add_start(time) {
	var row = cutlist_append_empty()
	row.className = "cut-active";
	$(row).find(".b-start")[0].value = time;
}

function add_end(time) {
	$(".cut-active .b-end")[0].value = time;
	$(".cut-active")[0].className = "";
}

function b_grab() {
	row = $(".cut-active").last();
	time = Math.round(player.getCurrentTime()*100.)/100.

	if (row.size() == 0) { // no cuts are active
		add_start(time);
		player.playVideo();
	} else {
		player.pauseVideo();
		add_end(time);
	}

	return;
}

function cutlist_make() {
	var cutlist = $("#cutlist tr").get()
		.filter(function(x) { return $(x).find("input").size() != 0 })
		.map(function(x) {
			return {
				start: +($(x).find("td input.b-start")[0].value),
				end: +($(x).find("td input.b-end")[0].value),
				id: player.getVideoData().video_id
			}
		})
		.filter(function(x) { return x.start != x.end; })

	return cutlist;
}

function b_share() {
	var cutlist = cutlist_make();
	var frag = frag_make(cutlist);
	history.pushState(null, "Video recut", "#" + frag);
}

var pending_cutlist = [];
function play_from_cutlist() {
	player.pauseVideo();
	pending_cutlist = cutlist_make();
	player.seekTo(0, false);
	player.seekTo(pending_cutlist[0].start, true);
	player.playVideo();
	playtime_check(0);
}

function playtime_check(t_now) {
	if (player.getCurrentTime() >= pending_cutlist[0].end) {
		// time to change time
		pending_cutlist.shift();

		if (pending_cutlist.length == 0) {
			if ($("#repeat-p")[0].checked) {
				// again again
				pending_cutlist = cutlist_make();
			} else {
				// done playing
				player.pauseVideo();
				return;
			}
		}
		player.seekTo(pending_cutlist[0].start, true);
	}
	// wait for a bit and check the time
	window.requestAnimationFrame(playtime_check);
}

function load_cutlist_from_url() {
	if (window.location.hash.length == 0)
		return;

	var cutlist = frag_parse(window.location.hash .replace("#", ""));
	cutlist.forEach(function(x) {
		add_start(x.start);
		add_end(x.end);
	});
	video_id = cutlist[0].id;
	load_video();
}

load_cutlist_from_url();
	</script>
  </body>
</html>