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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="de">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
+
<title>Sport Video App</title>
|
| 7 |
+
<link rel="manifest" href="/manifest.json" />
|
| 8 |
+
<!-- Optional: CSS-Datei einbinden -->
|
| 9 |
+
<link rel="stylesheet" href="/css/styles.css" />
|
| 10 |
+
</head>
|
| 11 |
+
<body>
|
| 12 |
+
<div id="root"></div>
|
| 13 |
+
<script>
|
| 14 |
+
if ('serviceWorker' in navigator) {
|
| 15 |
+
window.addEventListener('load', () => {
|
| 16 |
+
navigator.serviceWorker.register('/service-worker.js')
|
| 17 |
+
.then(registration => {
|
| 18 |
+
console.log('Service Worker registriert mit Scope:', registration.scope);
|
| 19 |
+
})
|
| 20 |
+
.catch(error => {
|
| 21 |
+
console.error('Service Worker Registrierung fehlgeschlagen:', error);
|
| 22 |
+
});
|
| 23 |
+
});
|
| 24 |
+
}
|
| 25 |
+
</script>
|
| 26 |
+
</body>
|
| 27 |
+
</html>
|
| 28 |
+
{
|
| 29 |
+
"short_name": "SportVideo",
|
| 30 |
+
"name": "Sport Video App",
|
| 31 |
+
"start_url": "/index.html",
|
| 32 |
+
"display": "standalone",
|
| 33 |
+
"background_color": "#ffffff",
|
| 34 |
+
"theme_color": "#000000",
|
| 35 |
+
"icons": [
|
| 36 |
+
{
|
| 37 |
+
"src": "/icons/icon-192x192.png",
|
| 38 |
+
"type": "image/png",
|
| 39 |
+
"sizes": "192x192"
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"src": "/icons/icon-512x512.png",
|
| 43 |
+
"type": "image/png",
|
| 44 |
+
"sizes": "512x512"
|
| 45 |
+
}
|
| 46 |
+
]
|
| 47 |
+
}
|
| 48 |
+
const CACHE_NAME = 'sport-video-app-cache-v1';
|
| 49 |
+
const urlsToCache = [
|
| 50 |
+
'/',
|
| 51 |
+
'/index.html',
|
| 52 |
+
'/css/styles.css',
|
| 53 |
+
'/js/main.js',
|
| 54 |
+
'/icons/icon-192x192.png',
|
| 55 |
+
'/icons/icon-512x512.png'
|
| 56 |
+
];
|
| 57 |
+
|
| 58 |
+
self.addEventListener('install', event => {
|
| 59 |
+
event.waitUntil(
|
| 60 |
+
caches.open(CACHE_NAME).then(cache => cache.addAll(urlsToCache))
|
| 61 |
+
);
|
| 62 |
+
});
|
| 63 |
+
|
| 64 |
+
self.addEventListener('fetch', event => {
|
| 65 |
+
event.respondWith(
|
| 66 |
+
caches.match(event.request)
|
| 67 |
+
.then(response => response || fetch(event.request))
|
| 68 |
+
);
|
| 69 |
+
});
|
| 70 |
+
// src/context/FFmpegContext.jsx
|
| 71 |
+
import React, { createContext, useContext, useState, useEffect } from 'react';
|
| 72 |
+
import { createFFmpeg } from '@ffmpeg/ffmpeg';
|
| 73 |
+
|
| 74 |
+
const FFmpegContext = createContext();
|
| 75 |
+
|
| 76 |
+
export const useFFmpeg = () => useContext(FFmpegContext);
|
| 77 |
+
|
| 78 |
+
export const FFmpegProvider = ({ children }) => {
|
| 79 |
+
const [ffmpeg] = useState(() => createFFmpeg({ log: true }));
|
| 80 |
+
const [isLoaded, setIsLoaded] = useState(false);
|
| 81 |
+
const [error, setError] = useState(null);
|
| 82 |
+
|
| 83 |
+
useEffect(() => {
|
| 84 |
+
const loadFFmpeg = async () => {
|
| 85 |
+
try {
|
| 86 |
+
await ffmpeg.load();
|
| 87 |
+
setIsLoaded(true);
|
| 88 |
+
} catch (err) {
|
| 89 |
+
setError(err);
|
| 90 |
+
}
|
| 91 |
+
};
|
| 92 |
+
loadFFmpeg();
|
| 93 |
+
}, [ffmpeg]);
|
| 94 |
+
|
| 95 |
+
return (
|
| 96 |
+
<FFmpegContext.Provider value={{ ffmpeg, isLoaded, error }}>
|
| 97 |
+
{children}
|
| 98 |
+
</FFmpegContext.Provider>
|
| 99 |
+
);
|
| 100 |
+
};
|
| 101 |
+
// src/context/VideoContext.jsx
|
| 102 |
+
import React, { createContext, useContext, useState } from 'react';
|
| 103 |
+
|
| 104 |
+
const VideoContext = createContext();
|
| 105 |
+
|
| 106 |
+
export const useVideo = () => useContext(VideoContext);
|
| 107 |
+
|
| 108 |
+
export const VideoProvider = ({ children }) => {
|
| 109 |
+
const [videos, setVideos] = useState([]);
|
| 110 |
+
|
| 111 |
+
const addVideo = (video) => {
|
| 112 |
+
setVideos(prevVideos => [...prevVideos, video]);
|
| 113 |
+
};
|
| 114 |
+
|
| 115 |
+
return (
|
| 116 |
+
<VideoContext.Provider value={{ videos, addVideo }}>
|
| 117 |
+
{children}
|
| 118 |
+
</VideoContext.Provider>
|
| 119 |
+
);
|
| 120 |
+
};
|
| 121 |
+
// src/components/VideoEditor.jsx
|
| 122 |
+
import React, { useState, useRef } from 'react';
|
| 123 |
+
import { useFFmpeg } from '../context/FFmpegContext';
|
| 124 |
+
import { useVideo } from '../context/VideoContext';
|
| 125 |
+
import { toast } from 'react-toastify';
|
| 126 |
+
import { CircularProgress } from '@mui/material';
|
| 127 |
+
|
| 128 |
+
const VideoEditor = () => {
|
| 129 |
+
const { ffmpeg, isLoaded, error } = useFFmpeg();
|
| 130 |
+
const { addVideo } = useVideo();
|
| 131 |
+
const [videoFile, setVideoFile] = useState(null);
|
| 132 |
+
const [processing, setProcessing] = useState(false);
|
| 133 |
+
const [progress, setProgress] = useState(0);
|
| 134 |
+
const videoRef = useRef();
|
| 135 |
+
|
| 136 |
+
const handleFileUpload = async (e) => {
|
| 137 |
+
const file = e.target.files[0];
|
| 138 |
+
if (!file) return;
|
| 139 |
+
|
| 140 |
+
if (!file.type.startsWith('video/')) {
|
| 141 |
+
toast.error('Nur Videoformate erlaubt (MP4, WebM, MOV)');
|
| 142 |
+
return;
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
if (file.size > process.env.REACT_APP_MAX_FILE_SIZE) {
|
| 146 |
+
toast.error('Maximale DateigrΓΆΓe: 100MB');
|
| 147 |
+
return;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
try {
|
| 151 |
+
setVideoFile(URL.createObjectURL(file));
|
| 152 |
+
} catch (err) {
|
| 153 |
+
toast.error('Fehler beim Verarbeiten der Datei');
|
| 154 |
+
}
|
| 155 |
+
};
|
| 156 |
+
|
| 157 |
+
const trimVideo = async () => {
|
| 158 |
+
if (!videoFile) {
|
| 159 |
+
toast.error('Kein Video ausgewΓ€hlt');
|
| 160 |
+
return;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
setProcessing(true);
|
| 164 |
+
try {
|
| 165 |
+
const inputName = 'input.mp4';
|
| 166 |
+
const outputName = 'output.mp4';
|
| 167 |
+
|
| 168 |
+
const response = await fetch(videoFile);
|
| 169 |
+
const buffer = await response.arrayBuffer();
|
| 170 |
+
ffmpeg.FS('writeFile', inputName, new Uint8Array(buffer));
|
| 171 |
+
|
| 172 |
+
ffmpeg.setProgress(({ ratio }) => setProgress(ratio));
|
| 173 |
+
|
| 174 |
+
// Trimme ab 2 Sekunden, Dauer 5 Sekunden
|
| 175 |
+
await ffmpeg.run('-i', inputName, '-ss', '00:00:02', '-t', '5', outputName);
|
| 176 |
+
|
| 177 |
+
const data = ffmpeg.FS('readFile', outputName);
|
| 178 |
+
const trimmedVideoURL = URL.createObjectURL(
|
| 179 |
+
new Blob([data.buffer], { type: 'video/mp4' })
|
| 180 |
+
);
|
| 181 |
+
|
| 182 |
+
addVideo(trimmedVideoURL);
|
| 183 |
+
toast.success('Video erfolgreich getrimmt');
|
| 184 |
+
} catch (err) {
|
| 185 |
+
console.error(err);
|
| 186 |
+
toast.error('Fehler beim Trimmen des Videos');
|
| 187 |
+
} finally {
|
| 188 |
+
setProcessing(false);
|
| 189 |
+
}
|
| 190 |
+
};
|
| 191 |
+
|
| 192 |
+
return (
|
| 193 |
+
<div>
|
| 194 |
+
<h2>Sport Video Editor</h2>
|
| 195 |
+
<input type="file" accept="video/*" onChange={handleFileUpload} />
|
| 196 |
+
{videoFile && <video ref={videoRef} src={videoFile} controls width="400" />}
|
| 197 |
+
{processing && <CircularProgress variant="determinate" value={progress * 100} />}
|
| 198 |
+
<button disabled={!isLoaded || processing} onClick={trimVideo}>
|
| 199 |
+
Video trimmen
|
| 200 |
+
</button>
|
| 201 |
+
{error && <p>FFmpeg Error: {error.message}</p>}
|
| 202 |
+
</div>
|
| 203 |
+
);
|
| 204 |
+
};
|
| 205 |
+
|
| 206 |
+
export default VideoEditor;
|
| 207 |
+
cd sport-video-app
|
| 208 |
+
npx create-react-app sport-video-app
|
| 209 |
+
node -v
|
| 210 |
+
npm -v
|
| 211 |
+
npm install
|
| 212 |
+
npm start
|
| 213 |
+
// public/service-worker.js
|
| 214 |
+
|
| 215 |
+
const CACHE_NAME = 'sport-video-app-cache-v1';
|
| 216 |
+
const urlsToCache = [
|
| 217 |
+
'/',
|
| 218 |
+
'/index.html',
|
| 219 |
+
'/css/styles.css', // falls du CSS nutzt
|
| 220 |
+
'/js/main.js', // falls weitere statische JS-Dateien vorhanden sind
|
| 221 |
+
'/icons/icon-192x192.png',
|
| 222 |
+
'/icons/icon-512x512.png'
|
| 223 |
+
];
|
| 224 |
+
|
| 225 |
+
self.addEventListener('install', event => {
|
| 226 |
+
event.waitUntil(
|
| 227 |
+
caches.open(CACHE_NAME).then(cache => cache.addAll(urlsToCache))
|
| 228 |
+
);
|
| 229 |
+
});
|
| 230 |
+
|
| 231 |
+
self.addEventListener('fetch', event => {
|
| 232 |
+
event.respondWith(
|
| 233 |
+
caches.match(event.request)
|
| 234 |
+
.then(response => response || fetch(event.request))
|
| 235 |
+
);
|
| 236 |
+
});
|
| 237 |
+
VideoEditor.jsxsport-video-app/
|
| 238 |
+
βββ public/
|
| 239 |
+
β βββ index.html
|
| 240 |
+
β βββ manifest.json
|
| 241 |
+
β βββ service-worker.js
|
| 242 |
+
β βββ icons/
|
| 243 |
+
β βββ icon-192x192.png
|
| 244 |
+
β βββ icon-512x512.png
|
| 245 |
+
βββ src/
|
| 246 |
+
β βββ components/
|
| 247 |
+
β β βββ VideoEditor.jsx
|
| 248 |
+
β βββ context/
|
| 249 |
+
β β βββ FFmpegContext.jsx
|
| 250 |
+
β β βββ VideoContext.jsx
|
| 251 |
+
β βββ App.jsx
|
| 252 |
+
β βββ index.js
|
| 253 |
+
βββ .env
|
| 254 |
+
βββ package.json
|
| 255 |
+
βββ README.md
|