MCPcopy Index your code
hub / github.com/CapSoftware/Cap / convertVideoToGif

Function convertVideoToGif

apps/web/components/tools/MediaFormatConverter.tsx:424–583  ·  view source on GitHub ↗
(inputFile: File)

Source from the content-addressed store, hash-verified

422 };
423
424 const convertVideoToGif = async (inputFile: File): Promise<void> => {
425 try {
426 const parser = await import("@remotion/media-parser");
427 const _webcodecs = await import("@remotion/webcodecs");
428
429 const _onProgress = ({
430 overallProgress,
431 }: {
432 overallProgress: number | null;
433 }) => {
434 if (overallProgress !== null) {
435 setProgress(Math.min(Math.round(overallProgress * 100), 99));
436 }
437 };
438
439 const controller = parser.mediaParserController
440 ? parser.mediaParserController()
441 : null;
442 parserControllerRef.current = controller;
443
444 console.log(`Starting video to GIF conversion`);
445 console.log(
446 `Input file: ${inputFile.name}, size: ${inputFile.size} bytes`,
447 );
448
449 const isCanvasSupported = !!document
450 .createElement("canvas")
451 .getContext("2d");
452 if (!isCanvasSupported) {
453 throw new Error(
454 "Your browser doesn't support canvas operations required for GIF conversion",
455 );
456 }
457
458 const metadata = await parser.parseMedia({
459 src: inputFile,
460 fields: {
461 durationInSeconds: true,
462 dimensions: true,
463 videoCodec: true,
464 },
465 });
466
467 console.log("Video metadata for GIF conversion:", metadata);
468
469 const originalWidth = metadata.dimensions?.width || 1920;
470 const originalHeight = metadata.dimensions?.height || 1080;
471 const maxWidth = gifMaxWidth;
472 const scale = originalWidth > maxWidth ? maxWidth / originalWidth : 1;
473 const targetWidth = Math.floor(originalWidth * scale);
474 const targetHeight = Math.floor(originalHeight * scale);
475
476 const GifModule = await import("gif.js");
477 const GIF = GifModule.default;
478
479 const videoElement = document.createElement("video");
480 videoElement.muted = true;
481 videoElement.playsInline = true;

Callers 1

convertFileFunction · 0.85

Calls 5

trackEventFunction · 0.90
onMethod · 0.80
pauseMethod · 0.80
captureFrameFunction · 0.70
resolveFunction · 0.50

Tested by

no test coverage detected