File size: 6,917 Bytes
20d9572
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
245
246
247
248
249
250
251
252
253
254
255
256
<!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" />
    <!-- Optional: CSS-Datei einbinden -->
    <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))
  );
});
// src/context/FFmpegContext.jsx
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>
  );
};
// src/context/VideoContext.jsx
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>
  );
};
// src/components/VideoEditor.jsx
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));

      // Trimme ab 2 Sekunden, Dauer 5 Sekunden
      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
// public/service-worker.js

const CACHE_NAME = 'sport-video-app-cache-v1';
const urlsToCache = [
  '/',
  '/index.html',
  '/css/styles.css', // falls du CSS nutzt
  '/js/main.js',     // falls weitere statische JS-Dateien vorhanden sind
  '/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