Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option (on by default) to strip audio files #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/kepubify/kepubify.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func main() {
noadddummytitlepage := pflag.Bool("no-add-dummy-titlepage", false, "Force-disables the dummy titlepage")
replace := pflag.StringArrayP("replace", "r", nil, "Find and replace on all html files (repeat any number of times) (format: find|replace)")
charset := pflag.String("charset", "utf-8", "Override the HTML charset (use \"auto\" to detect it from the content)")
leaveaudio := pflag.Bool("leave-audio", false, "Leave audio files in the book")

for _, flag := range []string{"smarten-punctuation", "css", "hyphenate", "no-hyphenate", "fullscreen-reading-fixes", "add-dummy-titlepage", "no-add-dummy-titlepage", "replace", "charset"} {
pflag.CommandLine.SetAnnotation(flag, "category", []string{"3.Conversion Options"})
Expand Down Expand Up @@ -108,6 +109,9 @@ func main() {
} else if *noadddummytitlepage {
opts = append(opts, kepub.ConverterOptionDummyTitlepage(false))
}
if *leaveaudio {
opts = append(opts, kepub.ConverterOptionLeaveAudio(true))
}
for _, r := range *replace {
spl := strings.SplitN(r, "|", 2)
if len(spl) != 2 {
Expand Down
8 changes: 8 additions & 0 deletions kepub/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ func (c *Converter) Convert(ctx context.Context, w io.Writer, r fs.FS) error {
}
}

if !c.leaveAudio {
for i, f := range files {
if c.IsAudioFile(f.Name) {
fileAct[i] = FileActionIgnore
}
}
}

// we'll manually create the mimetype file
if i, ok := fileIdx["mimetype"]; ok {
fileAct[i] = FileActionIgnore
Expand Down
10 changes: 10 additions & 0 deletions kepub/kepub.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type Converter struct {

// charset override
charset string // "auto" for auto-detection

// do not strip audio files (audiobook data)
leaveAudio bool
}

// ConverterOption configures a Converter.
Expand All @@ -44,6 +47,13 @@ func NewConverterWithOptions(opts ...ConverterOption) *Converter {
return c
}

// ConverterOptionLeaveAudio enables or disables leaving audio files in the book.
func ConverterOptionLeaveAudio(leave bool) ConverterOption {
return func(c *Converter) {
c.leaveAudio = leave
}
}

// ConverterOptionSmartypants enables smart punctuation.
func ConverterOptionSmartypants() ConverterOption {
return func(c *Converter) {
Expand Down
10 changes: 10 additions & 0 deletions kepub/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ func (c *Converter) TransformFileFilter(fn string) bool {
return false
}

func (c *Converter) IsAudioFile(fn string) bool {
switch path.Ext(fn) {
case ".mp3", ".m4a", ".m4b", ".m4p", ".m4r", ".m4v", ".mp4", ".aac", ".flac", ".ogg", ".oga", ".opus", ".webm", ".wav":
return true
case ".smil": // The media overlay
return true
}
return false
}

// TransformOPF transforms the OPF document for a KEPUB.
//
// * [mandatory] add the cover-image property to the cover.
Expand Down