@@ -11,6 +11,7 @@ import (
1111 "github.com/ashishb/wp2hugo/src/wp2hugo/internal/hugomanager/imageshrinker"
1212 "github.com/barasher/go-exiftool"
1313 "github.com/rs/zerolog/log"
14+ "github.com/samber/lo"
1415)
1516
1617func GetAudioBitrate (filePath string ) (* int , error ) {
@@ -33,18 +34,28 @@ func GetAudioBitrate(filePath string) (*int, error) {
3334
3435 bitRateAny , ok := metadata .Fields ["AudioBitrate" ]
3536 if ! ok {
36- return nil , fmt .Errorf ("AudioBitrate field not found in metadata for file: %s" , filePath )
37+ bitrateAny2 , ok2 := metadata .Fields ["AvgBitrate" ] // For .m4a files
38+ if ok2 {
39+ bitRateAny = bitrateAny2
40+ } else {
41+ log .Debug ().
42+ Str ("filePath" , filePath ).
43+ Interface ("metadataFields" , lo .Keys (metadata .Fields )).
44+ Msg ("Available metadata fields" )
45+ return nil , fmt .Errorf ("AudioBitrate field not found in metadata for file: %s" , filePath )
46+ }
3747 }
3848
3949 bitRateStr := fmt .Sprintf ("%v" , bitRateAny )
4050 bitRateStr = strings .ReplaceAll (bitRateStr , " kbps" , "000" )
4151 bitRateStr = strings .ReplaceAll (bitRateStr , " mbps" , "000000" )
42- bitRateInt , err := strconv .Atoi (bitRateStr )
52+ // Average bit rate is usually in bps, so we need to parse as a float
53+ bitRateFloat , err := strconv .ParseFloat (bitRateStr , 64 )
4354 if err != nil {
4455 return nil , fmt .Errorf ("unable to convert bitrate to int for file %s: %w" , filePath , err )
4556 }
4657
47- return & bitRateInt , nil
58+ return lo . ToPtr ( int ( bitRateFloat )) , nil
4859}
4960
5061// ReduceAudioBitrate processes a media file to reduce its audio bitrate
@@ -77,11 +88,16 @@ func ReduceAudioBitrate(ctx context.Context, inputFile string, outputFile string
7788 return fmt .Errorf ("FFmpeg failed: %w\n Output: %s" , err , output )
7889 }
7990
91+ originalSize := imageshrinker .GetFileSize (inputFile )
92+ newSize := imageshrinker .GetFileSize (outputFile )
93+ shrunkPct := 100.0 * (float64 (originalSize - newSize ) / float64 (originalSize ))
94+
8095 log .Info ().
8196 Str ("inputFile" , inputFile ).
8297 Str ("outputFile" , outputFile ).
83- Int64 ("inputFileSize" , imageshrinker .GetFileSize (inputFile )).
84- Int64 ("outputFileSize" , imageshrinker .GetFileSize (outputFile )).
98+ Int64 ("inputFileSize" , originalSize ).
99+ Int64 ("outputFileSize" , newSize ).
100+ Str ("shrinkBy" , fmt .Sprintf ("%.0f%%" , shrunkPct )).
85101 Str ("bitrate" , fmt .Sprintf ("%dkbps" , bitRate / 1000 )).
86102 Msg ("Audio bitrate reduction completed successfully" )
87103 return nil
0 commit comments