@@ -21,7 +21,7 @@ import { c } from "../ui/colors.js";
2121export const examples : Example [ ] = [
2222 [ "Generate the beat file for the current project" , "hyperframes beats" ] ,
2323 [ "Generate for a specific directory" , "hyperframes beats ./my-video" ] ,
24- [ "Beats of a single audio file" , "hyperframes audio music.mp3 --only beats --json" ] ,
24+ [ "Beats of a single audio or video file" , "hyperframes beats music.mp3 --json" ] ,
2525] ;
2626
2727function fail ( message : string ) : never {
@@ -96,24 +96,54 @@ function report(file: string, result: MusicBeatAnalysis, json: boolean): void {
9696 ) ;
9797}
9898
99+ async function analyzeFile ( audioPath : string , json : boolean ) : Promise < void > {
100+ const result = await analyzeMusicFromSignal ( await decode ( audioPath ) ) ;
101+ if ( result . beatTimes . length === 0 ) fail ( `No beats detected in ${ audioPath } .` ) ;
102+ if ( json ) {
103+ console . log (
104+ JSON . stringify (
105+ {
106+ ok : true ,
107+ file : audioPath ,
108+ count : result . beatTimes . length ,
109+ bpm : result . bpm ,
110+ beatTimes : result . beatTimes ,
111+ } ,
112+ null ,
113+ 2 ,
114+ ) ,
115+ ) ;
116+ return ;
117+ }
118+ console . log (
119+ c . success (
120+ `✓ ${ result . beatTimes . length } beats in ${ audioPath } (bpm ${ result . bpm ?? "?" } , ${ result . bpmConfidence } )` ,
121+ ) ,
122+ ) ;
123+ }
124+
99125export default defineCommand ( {
100126 meta : {
101127 name : "beats" ,
102- description : "Detect beats in the music track and write beats/<audio>.json" ,
128+ description :
129+ "Detect beats in the project music track (writes beats/<audio>.json) or in one audio file" ,
103130 } ,
104131 args : {
105- dir : { type : "positional" , description : "Project directory" , required : false } ,
132+ dir : {
133+ type : "positional" ,
134+ description : "Project directory, or one audio or video file to analyse" ,
135+ required : false ,
136+ } ,
106137 json : { type : "boolean" , description : "Output result as JSON" , default : false } ,
107138 } ,
108139 async run ( { args } ) {
109140 if ( args . dir && existsSync ( resolve ( args . dir ) ) && statSync ( resolve ( args . dir ) ) . isFile ( ) ) {
110- const file = JSON . stringify ( args . dir ) ;
111- fail (
112- / \. h t m l ? $ / i. test ( args . dir )
113- ? `${ args . dir } is a file; beats takes the project folder: hyperframes beats ${ JSON . stringify ( dirname ( args . dir ) ) } `
114- : `${ args . dir } is a file; beats takes a project folder. ` +
115- `For one audio or video file run: hyperframes audio ${ file } --only beats --json` ,
116- ) ;
141+ if ( / \. h t m l ? $ / i. test ( args . dir ) ) {
142+ fail (
143+ `${ args . dir } is a file; beats takes the project folder: hyperframes beats ${ JSON . stringify ( dirname ( args . dir ) ) } ` ,
144+ ) ;
145+ }
146+ return await analyzeFile ( resolve ( args . dir ) , Boolean ( args . json ) ) ;
117147 }
118148 const project = resolveProject ( args . dir ) ;
119149 const target = resolveMusicTarget ( project ) ;
0 commit comments