format.ts 1.1 KB

1234567891011121314151617181920212223242526272829
  1. /**
  2. * Smart timecode formatter.
  3. * Shows HH only if total duration >= 1 hour.
  4. * Shows MM only if total duration >= 1 minute.
  5. * Otherwise: SS:FF
  6. */
  7. export function formatTimecode(s: number, fps: number = 30, totalDuration?: number): string {
  8. if (!s || isNaN(s)) return '00:00:00:00';
  9. const h = Math.floor(s / 3600);
  10. const m = Math.floor((s % 3600) / 60);
  11. const sec = Math.floor(s % 60);
  12. const f = Math.round(s * fps) % fps;
  13. // Determine minimum display based on total duration
  14. const total = totalDuration ?? s;
  15. const totalH = Math.floor(total / 3600);
  16. const totalM = Math.floor((total % 3600) / 60);
  17. if (totalH > 0) {
  18. // Video is 1h+: show HH:MM:SS:FF
  19. return `${String(h).padStart(2,'0')}:${String(m).padStart(2,'0')}:${String(sec).padStart(2,'0')}:${String(f).padStart(2,'0')}`;
  20. }
  21. if (totalM > 0) {
  22. // Video is 1m+: show MM:SS:FF
  23. return `${String(m).padStart(2,'0')}:${String(sec).padStart(2,'0')}:${String(f).padStart(2,'0')}`;
  24. }
  25. // Under 1m: show SS:FF
  26. return `${String(sec).padStart(2,'0')}:${String(f).padStart(2,'0')}`;
  27. }