Skip to content

Commit 56bcd67

Browse files
Download trimmed video's
Merge pull request #59 from MichaelBelgium/trimming
2 parents 6476e9e + 40037a6 commit 56bcd67

File tree

4 files changed

+109
-4
lines changed

4 files changed

+109
-4
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"phpstan/phpstan": "^2.0"
2121
},
2222
"config": {
23-
"sort-packages": true
23+
"sort-packages": true,
24+
"process-timeout": 600
2425
}
2526
}

public/convert.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
if(isset($_GET["youtubelink"]) && !empty($_GET["youtubelink"]))
1212
{
1313
$youtubelink = $_GET["youtubelink"];
14+
$startAt = $_GET["startAt"] ?? null;
15+
$endAt = $_GET["endAt"] ?? null;
1416
$format = $_GET['format'] ?? 'mp3';
1517

1618
if(!in_array($format, POSSIBLE_FORMATS))
@@ -31,6 +33,36 @@
3133

3234
$exists = file_exists(env('DOWNLOAD_FOLDER').$id.".".$format);
3335

36+
if (!empty($startAt) || !empty($endAt))
37+
{
38+
if (!empty($startAt) && !empty($endAt))
39+
{
40+
if ((int)$startAt >= (int)$endAt)
41+
{
42+
http_response_code(400);
43+
die(json_encode(array("error" => true, "message" => "Invalid time range: startAt must be less than endAt")));
44+
}
45+
}
46+
47+
$query = parse_url($youtubelink, PHP_URL_QUERY);
48+
parse_str($query, $params);
49+
50+
if (!empty($startAt))
51+
$params['start'] = $startAt;
52+
53+
if (!empty($endAt))
54+
$params['end'] = $endAt;
55+
56+
$youtubelink = strtok($youtubelink, '?') . '?' . http_build_query($params);
57+
58+
if ($exists)
59+
{
60+
//todo this can probably go when youtube-dl-php supports --force-overwrites options
61+
unlink(env('DOWNLOAD_FOLDER').$id.".".$format);
62+
$exists = false;
63+
}
64+
}
65+
3466
if(env('DOWNLOAD_MAX_LENGTH', 0) > 0 || $exists)
3567
{
3668
try {
@@ -69,6 +101,9 @@
69101
->cookies(file_exists(env('COOKIE_FILE')) ? env('COOKIE_FILE') : null)
70102
->url($youtubelink);
71103

104+
if (!empty($startAt) || !empty($endAt))
105+
$options = $options->downloadSections('*from-url');
106+
72107
if($format == 'mp3')
73108
{
74109
$options = $options->extractAudio(true)

public/index.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<div class="row">
2424
<div class="col-lg-8">
2525
<div class="form-floating mb-3">
26-
<input type="text" name="youtubelink" class="form-control" id="link" required placeholder="youtube.com" />
26+
<input type="text" name="youtubelink" class="form-control" id="link" onchange="fillStartEnd()" onkeyup="fillStartEnd()" required placeholder=" " />
2727
<label for="link">Youtube url</label>
2828
</div>
2929
</div>
@@ -39,7 +39,35 @@
3939
</div>
4040
</div>
4141

42-
<button type="submit" class="btn btn-outline-primary"><i class="fas fa-sync-alt"></i> Convert</button>
42+
<div class="row">
43+
<div class="col-lg-12">
44+
<div class="input-group">
45+
<span class="input-group-text">
46+
<i class="fa-solid fa-forward-step"></i>
47+
</span>
48+
49+
<div class="form-floating">
50+
<input type="number" class="form-control" min="0" name="startAt" id="startAt" placeholder=" " />
51+
<label for="startAt">Start</label>
52+
</div>
53+
54+
<div class="form-floating">
55+
<input type="number" class="form-control" min="0" name="endAt" id="endAt" placeholder=" " />
56+
<label for="endAt">End</label>
57+
</div>
58+
59+
<span class="input-group-text">
60+
<i class="fa-solid fa-backward-step"></i>
61+
</span>
62+
</div>
63+
<div class="form-text">
64+
Specify start and/or end times <b>(in seconds)</b> to trim the video. Leave both empty to download the complete video.<br/>
65+
Start time only: downloads from that point to the end. End time only: downloads from the beginning to that point.
66+
</div>
67+
</div>
68+
</div>
69+
70+
<button type="submit" class="btn btn-outline-primary mt-4"><i class="fas fa-sync-alt"></i> Convert</button>
4371
</form>
4472
</div>
4573
</div>

public/script.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ document.addEventListener('DOMContentLoaded', () => {
99
const submitButton = frmConvert.querySelector("button[type=submit]");
1010
const link = document.getElementById('link').value;
1111
const format = document.getElementById('format').value;
12+
const startAt = document.getElementById('startAt').value;
13+
const endAt = document.getElementById('endAt').value;
1214

1315
submitButton.classList.add("disabled");
1416
submitButton.innerHTML = "<i class=\"fas fa-spin fa-sync-alt\"></i> Converting...";
1517

16-
const endpoint = `${frmConvert.getAttribute("action")}?youtubelink=${link}&format=${format}`;
18+
let endpoint = `${frmConvert.getAttribute("action")}?youtubelink=${link}&format=${format}`;
19+
20+
if (startAt.length > 0)
21+
endpoint += `&startAt=${startAt}`;
22+
23+
if (endAt.length > 0)
24+
endpoint += `&endAt=${endAt}`;
1725

1826
fetch(endpoint)
1927
.then(response => response.json())
@@ -168,4 +176,37 @@ function handleInfoResponse(data, submitButton)
168176
tableCells[9].innerText = data.title;
169177
tableCells[10].innerHTML = `<a target='_blank' href='${data.url}'>${data.url}</a>`;
170178
}
179+
}
180+
181+
function fillStartEnd()
182+
{
183+
const elLink = document.getElementById('link');
184+
const elStart = document.getElementById('startAt');
185+
const elEnd = document.getElementById('endAt');
186+
187+
const url = new URL(elLink.value);
188+
const start = url.searchParams.get("start") || url.searchParams.get("t");
189+
const end = url.searchParams.get("end");
190+
191+
if (start && start.length > 0)
192+
{
193+
elStart.value = start;
194+
elStart.setAttribute('readonly', 'readonly');
195+
}
196+
else
197+
{
198+
elStart.value = '';
199+
elStart.removeAttribute('readonly');
200+
}
201+
202+
if (end && end.length > 0)
203+
{
204+
elEnd.value = end;
205+
elEnd.setAttribute('readonly', 'readonly');
206+
}
207+
else
208+
{
209+
elEnd.value = '';
210+
elEnd.removeAttribute('readonly');
211+
}
171212
}

0 commit comments

Comments
 (0)