Skip to content

Commit ce42411

Browse files
committed
feat(hugomanager): improve shrink-audio-files
1. Add more logging 2. Handle `m4a` files better
1 parent 6775346 commit ce42411

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/wp2hugo/cmd/hugomanager/cmd/shrink_audio.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ func shrinkAudioFiles(ctx context.Context, hugoDir string, maxBitRate int, updat
5151
return err
5252
}
5353

54-
if *bitrate <= maxBitRate {
54+
// If you shrink a file to 48kbps, sometimes the resulting bitrate is 64kbps,
55+
// so we give a 33% buffer to avoid unnecessary re-processing
56+
if *bitrate <= (maxBitRate * 4 / 3) {
5557
log.Trace().
5658
Str("filePath", filePath).
5759
Int("bitrate", *bitrate).
60+
Int("maxBitRate", maxBitRate).
5861
Msg("Audio file within bitrate limits, skipping")
5962
return nil
6063
}

src/wp2hugo/internal/hugomanager/mediafileshrinker/audio_file_helper.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1617
func 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\nOutput: %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

Comments
 (0)