如何在JavaScript中跳过音频沉默

发布于 2025-01-22 20:45:03 字数 190 浏览 3 评论 0原文

我已经对此进行了很多搜索,但是我找不到任何类似内容的可靠代码段,这些代码片段解释了这将如何工作。

Google Podcasts Web应用程序可以做到这一点,我想了解这对于我正在构建的Web应用程序的工作原理。

许多播客应用程序都可以让您跳过沉默,即使很小的半秒钟长则沉默。

我该如何使用JavaScript执行此操作?

I have searched a lot about this but I can't find any solid code snippet of anything like that that explains how this would work.

Google Podcasts web app does this and I would like to learn how this works for a web app I'm building.

A lot of podcast apps let you skip silences even as small as half a second long silences.

How can I do this with JavaScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长梦不多时 2025-01-29 20:45:03
<html>
<!DOCTYPE html>
<html>
<head>
<title>Skip Silence in Video</title>
</head>
<body>
<video id="videoPlayer" controls></video>
<script>
function skipSilence() {
  const video = document.getElementById('videoPlayer');
  const canvas = document.createElement('canvas');
  const context = canvas.getContext('2d');
  const outputChunks = [];

  video.addEventListener('loadedmetadata', () => {
    const { videoWidth, videoHeight } = video;
    canvas.width = videoWidth;
    canvas.height = videoHeight;
  });
  video.addEventListener('play', () => {
    const fps = 30; // Adjust the desired frame rate
    setInterval(() => {
      if (video.paused || video.ended) return;
      context.drawImage(video, 0, 0, canvas.width, canvas.height);
      const frameData = context.getImageData(0, 0, canvas.width, canvas.height).data;
       // Check if the frame contains silence (all black pixels)
      const isSilent = frameData.every((value, index) => {
        return index % 4 !== 3 || value === 0;
      });
      if (!isSilent) {
        outputChunks.push(canvas.toDataURL());
      }
    }, 1000 / fps);
  });
  video.addEventListener('ended', () => {
    const outputVideo = document.createElement('video');
    outputVideo.controls = true;
    outputChunks.forEach((chunk) => {
      const source = document.createElement('source');
      source.src = chunk;
      outputVideo.appendChild(source);
    });
    document.body.appendChild(outputVideo);
  });
   // Specify the path to your video file
  video.src = 'G:/Fking/LECTURS/chemmmmm/test.mp4';
  video.load();
  video.play();
}
     // Call the function to skip silence in the video
    skipSilence();
  </script>
</body>
</html>

这样的东西也许?

<html>
<!DOCTYPE html>
<html>
<head>
<title>Skip Silence in Video</title>
</head>
<body>
<video id="videoPlayer" controls></video>
<script>
function skipSilence() {
  const video = document.getElementById('videoPlayer');
  const canvas = document.createElement('canvas');
  const context = canvas.getContext('2d');
  const outputChunks = [];

  video.addEventListener('loadedmetadata', () => {
    const { videoWidth, videoHeight } = video;
    canvas.width = videoWidth;
    canvas.height = videoHeight;
  });
  video.addEventListener('play', () => {
    const fps = 30; // Adjust the desired frame rate
    setInterval(() => {
      if (video.paused || video.ended) return;
      context.drawImage(video, 0, 0, canvas.width, canvas.height);
      const frameData = context.getImageData(0, 0, canvas.width, canvas.height).data;
       // Check if the frame contains silence (all black pixels)
      const isSilent = frameData.every((value, index) => {
        return index % 4 !== 3 || value === 0;
      });
      if (!isSilent) {
        outputChunks.push(canvas.toDataURL());
      }
    }, 1000 / fps);
  });
  video.addEventListener('ended', () => {
    const outputVideo = document.createElement('video');
    outputVideo.controls = true;
    outputChunks.forEach((chunk) => {
      const source = document.createElement('source');
      source.src = chunk;
      outputVideo.appendChild(source);
    });
    document.body.appendChild(outputVideo);
  });
   // Specify the path to your video file
  video.src = 'G:/Fking/LECTURS/chemmmmm/test.mp4';
  video.load();
  video.play();
}
     // Call the function to skip silence in the video
    skipSilence();
  </script>
</body>
</html>

Something like this maybe ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文