44 lines
1.7 KiB
Racket
44 lines
1.7 KiB
Racket
#lang racket/base
|
|
|
|
(require racket/file
|
|
racket/path)
|
|
|
|
(provide convert-flac-to-target-in-place)
|
|
|
|
(define (temp-output-path input-path)
|
|
(define-values (base name dir?) (split-path input-path))
|
|
(define name-str (path->string name))
|
|
(build-path base (format ".~a.tmp-~a.flac" name-str (current-inexact-milliseconds))))
|
|
|
|
(define (settings->hash max-sample-rate compression-level)
|
|
(make-immutable-hash
|
|
(list (cons 'target-sample-rate max-sample-rate)
|
|
(cons 'compression-level compression-level))))
|
|
|
|
(define (delete-file/quiet path)
|
|
(with-handlers ([exn:fail? (lambda (_) #f)])
|
|
(when (file-exists? path) (delete-file path))))
|
|
|
|
(define (convert-flac-to-target-in-place input-path max-sample-rate compression-level
|
|
#:progress-callback [progress-callback #f])
|
|
(define tmp-path (temp-output-path input-path))
|
|
(define audio-encode (dynamic-require 'racket-audio/audio-encoder 'audio-encode))
|
|
(with-handlers ([exn:break?
|
|
(lambda (e)
|
|
(delete-file/quiet tmp-path)
|
|
(raise e))]
|
|
[exn:fail?
|
|
(lambda (e)
|
|
(delete-file/quiet tmp-path)
|
|
(error 'convert-flac-to-target-in-place
|
|
"conversion failed for ~a: ~a" input-path (exn-message e)))])
|
|
(define result
|
|
(audio-encode (path->string input-path)
|
|
(path->string tmp-path)
|
|
(settings->hash max-sample-rate compression-level)
|
|
#:encoder 'flac
|
|
#:copy-tags? #t
|
|
#:progress-callback progress-callback))
|
|
(rename-file-or-directory tmp-path input-path #t)
|
|
result))
|