You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求更优雅的文件压缩上传进度更新通知处理方案

Optimizing Progress Tracking for FFmpeg Compression

Hey Daniel, let's clean up that repetitive progress tracking code! Your current approach with separate boolean variables gets the job done, but it becomes messy and hard to maintain if you ever need to add more progress checkpoints. Here are two cleaner, more scalable alternatives:

Option 1: Use a Checkpoint Array (Flexible for Custom Thresholds)

This approach lets you easily add or remove progress milestones without rewriting conditional logic. We'll track the highest threshold we've already notified the user about, then check against a list of defined checkpoints:

// Define your progress checkpoints in an array (easy to modify!)
const progressCheckpoints = [25, 50, 75];
// Track the highest threshold we've already notified for
let lastNotifiedThreshold = 0;

ffmpeg(file.path)
  .addOption('-hls_time', 10)
  .addOption('-hls_list_size', 0)
  .addOption('-f', 'hls')
  .on('progress', (progress) => {
    // Find the first checkpoint that's above our last notified threshold and has been passed
    const nextCheckpoint = progressCheckpoints.find(threshold => 
      threshold > lastNotifiedThreshold && progress.percent > threshold
    );

    if (nextCheckpoint) {
      notifyUser(progress);
      lastNotifiedThreshold = nextCheckpoint;
    }
  });

Why this works better:

  • Scalability: Want to add a 10% or 90% checkpoint? Just add it to the progressCheckpoints array—no extra variables or conditionals needed.
  • Less redundancy: No more managing separate boolean flags for each threshold.
  • Clearer logic: The intent of checking progress milestones is immediately obvious to anyone reading the code.

Option 2: Interval-Based Tracking (Perfect for Fixed 25% Steps)

If you know you'll always stick to 25% intervals, this even simpler approach uses integer division to track which "quarter" the progress is in:

// Track which 25% interval we're in (0 = 0-24%, 1=25-49%, 2=50-74%, 3=75-99%)
let currentProgressQuarter = 0;

ffmpeg(file.path)
  .addOption('-hls_time', 10)
  .addOption('-hls_list_size', 0)
  .addOption('-f', 'hls')
  .on('progress', (progress) => {
    // Calculate the current quarter (floor division by 25)
    const newQuarter = Math.floor(progress.percent / 25);
    
    // Only notify if we've moved to a new quarter, and we haven't passed the final 75% checkpoint
    if (newQuarter > currentProgressQuarter && newQuarter <= 3) {
      notifyUser(progress);
      currentProgressQuarter = newQuarter;
    }
  });

Why this works better:

  • Ultra concise: No need to define individual checkpoints—math handles the interval logic automatically.
  • Easy to adjust: If you later want 10% intervals, just change the divisor to 10 and adjust the upper limit (e.g., newQuarter <=9).

Both approaches eliminate the messy boolean flags while keeping your core goal: avoiding frequent updates by only notifying users at key progress milestones.

内容的提问来源于stack exchange,提问作者Daniel Shen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 22:17:39