|
<!DOCTYPE html> |
|
<html lang="de"> |
|
<head> |
|
<meta charset="UTF-8" /> |
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
|
<title>Sport Video App</title> |
|
<link rel="manifest" href="/manifest.json" /> |
|
|
|
<link rel="stylesheet" href="/css/styles.css" /> |
|
</head> |
|
<body> |
|
<div id="root"></div> |
|
<script> |
|
if ('serviceWorker' in navigator) { |
|
window.addEventListener('load', () => { |
|
navigator.serviceWorker.register('/service-worker.js') |
|
.then(registration => { |
|
console.log('Service Worker registriert mit Scope:', registration.scope); |
|
}) |
|
.catch(error => { |
|
console.error('Service Worker Registrierung fehlgeschlagen:', error); |
|
}); |
|
}); |
|
} |
|
</script> |
|
</body> |
|
</html> |
|
{ |
|
"short_name": "SportVideo", |
|
"name": "Sport Video App", |
|
"start_url": "/index.html", |
|
"display": "standalone", |
|
"background_color": "#ffffff", |
|
"theme_color": "#000000", |
|
"icons": [ |
|
{ |
|
"src": "/icons/icon-192x192.png", |
|
"type": "image/png", |
|
"sizes": "192x192" |
|
}, |
|
{ |
|
"src": "/icons/icon-512x512.png", |
|
"type": "image/png", |
|
"sizes": "512x512" |
|
} |
|
] |
|
} |
|
const CACHE_NAME = 'sport-video-app-cache-v1'; |
|
const urlsToCache = [ |
|
'/', |
|
'/index.html', |
|
'/css/styles.css', |
|
'/js/main.js', |
|
'/icons/icon-192x192.png', |
|
'/icons/icon-512x512.png' |
|
]; |
|
|
|
self.addEventListener('install', event => { |
|
event.waitUntil( |
|
caches.open(CACHE_NAME).then(cache => cache.addAll(urlsToCache)) |
|
); |
|
}); |
|
|
|
self.addEventListener('fetch', event => { |
|
event.respondWith( |
|
caches.match(event.request) |
|
.then(response => response || fetch(event.request)) |
|
); |
|
}); |
|
|
|
import React, { createContext, useContext, useState, useEffect } from 'react'; |
|
import { createFFmpeg } from '@ffmpeg/ffmpeg'; |
|
|
|
const FFmpegContext = createContext(); |
|
|
|
export const useFFmpeg = () => useContext(FFmpegContext); |
|
|
|
export const FFmpegProvider = ({ children }) => { |
|
const [ffmpeg] = useState(() => createFFmpeg({ log: true })); |
|
const [isLoaded, setIsLoaded] = useState(false); |
|
const [error, setError] = useState(null); |
|
|
|
useEffect(() => { |
|
const loadFFmpeg = async () => { |
|
try { |
|
await ffmpeg.load(); |
|
setIsLoaded(true); |
|
} catch (err) { |
|
setError(err); |
|
} |
|
}; |
|
loadFFmpeg(); |
|
}, [ffmpeg]); |
|
|
|
return ( |
|
<FFmpegContext.Provider value={{ ffmpeg, isLoaded, error }}> |
|
{children} |
|
</FFmpegContext.Provider> |
|
); |
|
}; |
|
|
|
import React, { createContext, useContext, useState } from 'react'; |
|
|
|
const VideoContext = createContext(); |
|
|
|
export const useVideo = () => useContext(VideoContext); |
|
|
|
export const VideoProvider = ({ children }) => { |
|
const [videos, setVideos] = useState([]); |
|
|
|
const addVideo = (video) => { |
|
setVideos(prevVideos => [...prevVideos, video]); |
|
}; |
|
|
|
return ( |
|
<VideoContext.Provider value={{ videos, addVideo }}> |
|
{children} |
|
</VideoContext.Provider> |
|
); |
|
}; |
|
|
|
import React, { useState, useRef } from 'react'; |
|
import { useFFmpeg } from '../context/FFmpegContext'; |
|
import { useVideo } from '../context/VideoContext'; |
|
import { toast } from 'react-toastify'; |
|
import { CircularProgress } from '@mui/material'; |
|
|
|
const VideoEditor = () => { |
|
const { ffmpeg, isLoaded, error } = useFFmpeg(); |
|
const { addVideo } = useVideo(); |
|
const [videoFile, setVideoFile] = useState(null); |
|
const [processing, setProcessing] = useState(false); |
|
const [progress, setProgress] = useState(0); |
|
const videoRef = useRef(); |
|
|
|
const handleFileUpload = async (e) => { |
|
const file = e.target.files[0]; |
|
if (!file) return; |
|
|
|
if (!file.type.startsWith('video/')) { |
|
toast.error('Nur Videoformate erlaubt (MP4, WebM, MOV)'); |
|
return; |
|
} |
|
|
|
if (file.size > process.env.REACT_APP_MAX_FILE_SIZE) { |
|
toast.error('Maximale DateigrΓΆΓe: 100MB'); |
|
return; |
|
} |
|
|
|
try { |
|
setVideoFile(URL.createObjectURL(file)); |
|
} catch (err) { |
|
toast.error('Fehler beim Verarbeiten der Datei'); |
|
} |
|
}; |
|
|
|
const trimVideo = async () => { |
|
if (!videoFile) { |
|
toast.error('Kein Video ausgewΓ€hlt'); |
|
return; |
|
} |
|
|
|
setProcessing(true); |
|
try { |
|
const inputName = 'input.mp4'; |
|
const outputName = 'output.mp4'; |
|
|
|
const response = await fetch(videoFile); |
|
const buffer = await response.arrayBuffer(); |
|
ffmpeg.FS('writeFile', inputName, new Uint8Array(buffer)); |
|
|
|
ffmpeg.setProgress(({ ratio }) => setProgress(ratio)); |
|
|
|
|
|
await ffmpeg.run('-i', inputName, '-ss', '00:00:02', '-t', '5', outputName); |
|
|
|
const data = ffmpeg.FS('readFile', outputName); |
|
const trimmedVideoURL = URL.createObjectURL( |
|
new Blob([data.buffer], { type: 'video/mp4' }) |
|
); |
|
|
|
addVideo(trimmedVideoURL); |
|
toast.success('Video erfolgreich getrimmt'); |
|
} catch (err) { |
|
console.error(err); |
|
toast.error('Fehler beim Trimmen des Videos'); |
|
} finally { |
|
setProcessing(false); |
|
} |
|
}; |
|
|
|
return ( |
|
<div> |
|
<h2>Sport Video Editor</h2> |
|
<input type="file" accept="video/*" onChange={handleFileUpload} /> |
|
{videoFile && <video ref={videoRef} src={videoFile} controls width="400" />} |
|
{processing && <CircularProgress variant="determinate" value={progress * 100} />} |
|
<button disabled={!isLoaded || processing} onClick={trimVideo}> |
|
Video trimmen |
|
</button> |
|
{error && <p>FFmpeg Error: {error.message}</p>} |
|
</div> |
|
); |
|
}; |
|
|
|
export default VideoEditor; |
|
cd sport-video-app |
|
npx create-react-app sport-video-app |
|
node -v |
|
npm -v |
|
npm install |
|
npm start |
|
|
|
|
|
const CACHE_NAME = 'sport-video-app-cache-v1'; |
|
const urlsToCache = [ |
|
'/', |
|
'/index.html', |
|
'/css/styles.css', |
|
'/js/main.js', |
|
'/icons/icon-192x192.png', |
|
'/icons/icon-512x512.png' |
|
]; |
|
|
|
self.addEventListener('install', event => { |
|
event.waitUntil( |
|
caches.open(CACHE_NAME).then(cache => cache.addAll(urlsToCache)) |
|
); |
|
}); |
|
|
|
self.addEventListener('fetch', event => { |
|
event.respondWith( |
|
caches.match(event.request) |
|
.then(response => response || fetch(event.request)) |
|
); |
|
}); |
|
VideoEditor.jsxsport-video-app/ |
|
βββ public/ |
|
β βββ index.html |
|
β βββ manifest.json |
|
β βββ service-worker.js |
|
β βββ icons/ |
|
β βββ icon-192x192.png |
|
β βββ icon-512x512.png |
|
βββ src/ |
|
β βββ components/ |
|
β β βββ VideoEditor.jsx |
|
β βββ context/ |
|
β β βββ FFmpegContext.jsx |
|
β β βββ VideoContext.jsx |
|
β βββ App.jsx |
|
β βββ index.js |
|
βββ .env |
|
βββ package.json |
|
βββ README.md |
|
|