-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideo Aspect Ratio Stretcher.user.js
More file actions
215 lines (189 loc) · 7.21 KB
/
Copy pathVideo Aspect Ratio Stretcher.user.js
File metadata and controls
215 lines (189 loc) · 7.21 KB
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
// ==UserScript==
// @name Video Aspect Ratio Stretcher
// @namespace http://tampermonkey.net/
// @version 2.0
// @description Dynamically stretch videos to fill a 16:10 screen with improved aspect ratio detection
// @author You
// @match *://*/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Configuration
const TARGET_ASPECT_RATIO = 16/10; // Target aspect ratio (16:10)
const ASPECT_RATIO_THRESHOLD = 0.05; // Threshold for aspect ratio detection
const STRETCH_MODES = {
NONE: 0,
HORIZONTAL: 1,
VERTICAL: 2,
CUSTOM: 3
};
// State variables
let currentMode = STRETCH_MODES.NONE;
let scaleX = 1;
let scaleY = 1;
let normalFullscreen = false;
// Function to determine the best stretching mode for a given video
function determineStretchMode(video) {
const videoAspect = video.videoWidth / video.videoHeight;
// Skip stretching for vertical videos (e.g., 9:16)
if (videoAspect < 0.75) { // 9:16 = 0.5625, so we use a slightly higher threshold
return {
mode: STRETCH_MODES.NONE,
scaleX: 1,
scaleY: 1
};
}
// If already close to target ratio, no stretching needed
if (Math.abs(videoAspect - TARGET_ASPECT_RATIO) < ASPECT_RATIO_THRESHOLD) {
return {
mode: STRETCH_MODES.NONE,
scaleX: 1,
scaleY: 1
};
}
// Determine which way to stretch
if (videoAspect > TARGET_ASPECT_RATIO) {
// Wider than 16:10 (like 16:9, 21:9) - stretch vertically
const scaleFactor = videoAspect / TARGET_ASPECT_RATIO;
return {
mode: STRETCH_MODES.VERTICAL,
scaleX: 1,
scaleY: scaleFactor
};
} else {
// Narrower than 16:10 (like 4:3) - stretch horizontally
const scaleFactor = TARGET_ASPECT_RATIO / videoAspect;
return {
mode: STRETCH_MODES.HORIZONTAL,
scaleX: scaleFactor,
scaleY: 1
};
}
}
// Function to apply stretching to video
function applyStretch(video, stretchX, stretchY) {
if (!video) return;
video.style.transform = `scaleX(${stretchX.toFixed(3)}) scaleY(${stretchY.toFixed(3)})`;
video.style.transformOrigin = 'center center';
}
// Toggle video stretching
function toggleStretchVideo(forceMode = null) {
const video = document.querySelector('video');
if (!video || video.videoWidth === 0 || video.videoHeight === 0) {
return;
}
if (forceMode !== null) {
currentMode = forceMode;
} else {
// Cycle through modes
currentMode = (currentMode + 1) % Object.keys(STRETCH_MODES).length;
}
switch (currentMode) {
case STRETCH_MODES.NONE:
video.style.transform = '';
break;
case STRETCH_MODES.CUSTOM:
applyStretch(video, scaleX, scaleY);
break;
default:
const stretchInfo = determineStretchMode(video);
scaleX = stretchInfo.scaleX;
scaleY = stretchInfo.scaleY;
applyStretch(video, scaleX, scaleY);
break;
}
}
// Handle keyboard shortcuts
document.addEventListener('keydown', function (e) {
const video = document.querySelector('video');
if (!video) return;
// Toggle stretching with 'u' key
if (e.key === 'u') {
toggleStretchVideo();
}
// Fine-tune stretching with Ctrl+arrows
if (e.ctrlKey) {
const step = 0.01;
let changed = true;
if (e.key === 'ArrowUp') {
scaleY += step;
} else if (e.key === 'ArrowDown') {
scaleY -= step;
} else if (e.key === 'ArrowRight') {
scaleX += step;
} else if (e.key === 'ArrowLeft') {
scaleX -= step;
} else if (e.key === 'r') {
// Reset scales
scaleX = 1;
scaleY = 1;
} else {
changed = false;
}
if (changed) {
// Ensure we don't go below minimum scale
scaleX = Math.max(0.5, scaleX);
scaleY = Math.max(0.5, scaleY);
// Switch to custom mode
currentMode = STRETCH_MODES.CUSTOM;
applyStretch(video, scaleX, scaleY);
e.preventDefault();
}
}
});
// Auto-apply stretching when entering fullscreen
document.addEventListener('fullscreenchange', function () {
const video = document.querySelector('video');
if (!video) return;
const isYouTube = window.location.hostname.includes('youtube.com');
const fullscreenElement = document.fullscreenElement ||
document.mozFullScreenElement ||
document.webkitFullscreenElement;
if (!normalFullscreen) {
if (fullscreenElement && currentMode === STRETCH_MODES.NONE) {
// Wait a bit longer for YouTube to ensure video dimensions are available
const delay = isYouTube ? 500 : 0;
setTimeout(() => {
// Check if video dimensions are available
if (video.videoWidth > 0 && video.videoHeight > 0) {
toggleStretchVideo(1); // Auto mode
}
}, delay);
} else if (!fullscreenElement && currentMode !== STRETCH_MODES.NONE) {
// Reset when exiting fullscreen
currentMode = STRETCH_MODES.NONE;
if (video) {
video.style.transform = '';
}
}
}
});
// Monitor for video element changes/loads
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
const videos = document.querySelectorAll('video');
videos.forEach(video => {
if (!video.hasAttribute('data-stretcher-initialized')) {
video.setAttribute('data-stretcher-initialized', 'true');
// Handle videos that load after page load
video.addEventListener('loadedmetadata', function() {
// Auto-apply in fullscreen mode
if (document.fullscreenElement &&
document.fullscreenElement.contains(video) &&
currentMode === STRETCH_MODES.NONE) {
toggleStretchVideo(1);
}
});
}
});
}
});
});
// Start observing document for video elements
observer.observe(document.body, {
childList: true,
subtree: true
});
})();