1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// observations publishes an audio format: one person reading someone else's
// text out loud and thinking about it. See observations(7) for what the format
// is and why the pipeline is shaped this way, and observations(1) for the
// reference.
package main

import (
	"flag"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"
)

const usage = `observations — publish audio readings of other people's texts

usage:
  observations add [flags] <recording.m4a>   ingest a recording
  observations render [flags]                render the site
  observations list [flags]                  list episodes

Run 'observations <command> -h' for the flags of a command.
See observations(1) for the full reference.
`

func main() {
	if len(os.Args) < 2 {
		fmt.Fprint(os.Stderr, usage)
		os.Exit(2)
	}
	var err error
	switch os.Args[1] {
	case "add":
		err = cmdAdd(os.Args[2:])
	case "render":
		err = cmdRender(os.Args[2:])
	case "list":
		err = cmdList(os.Args[2:])
	case "-h", "--help", "help":
		fmt.Print(usage)
		return
	default:
		fmt.Fprintf(os.Stderr, "observations: unknown command %q\n\n%s", os.Args[1], usage)
		os.Exit(2)
	}
	if err != nil {
		fmt.Fprintf(os.Stderr, "observations: %v\n", err)
		os.Exit(1)
	}
}

// dirs holds the three locations the tool works with. They are flags rather
// than constants so the tool can be run against a scratch tree in tests and
// from a nix build.
type dirs struct {
	episodes string
	audio    string
	out      string
}

func (d *dirs) flags(fs *flag.FlagSet) {
	fs.StringVar(&d.episodes, "episodes", "episodes", "directory of episode metadata (in git)")
	fs.StringVar(&d.audio, "audio", "audio", "directory of audio files (NOT in git)")
	fs.StringVar(&d.out, "out", "", "directory to render the site into")
}

func cmdAdd(args []string) error {
	fs := flag.NewFlagSet("add", flag.ExitOnError)
	var d dirs
	d.flags(fs)
	var (
		title        = fs.String("source-title", "", "title of the text being read (required)")
		url          = fs.String("source-url", "", "URL of the text being read (required)")
		author       = fs.String("source-author", "", "author of the text (required)")
		published    = fs.String("source-published", "", "publication date of the text, YYYY-MM-DD (required)")
		site         = fs.String("source-site", "", "publication the text appeared in")
		note         = fs.String("source-note", "", "what this text is doing in the episode (\"intro only\")")
		episodeTitle = fs.String("title", "", "title of the episode (default: the source's title)")
		recorded     = fs.String("recorded", "", "date of the recording, YYYY-MM-DD (default: the file's mtime)")
		notes        = fs.String("notes", "", "prose shown on the episode page")
		number       = fs.Int("number", 0, "episode number (default: next free)")
		noTranscript = fs.Bool("no-transcript", false, "accept a recording that carries no transcript track")
		ffprobeBin   = fs.String("ffprobe", "ffprobe", "path to ffprobe")
		ffmpegBin    = fs.String("ffmpeg", "ffmpeg", "path to ffmpeg")
	)
	fs.Usage = func() {
		fmt.Fprint(fs.Output(), "usage: observations add [flags] <recording.m4a>\n\n")
		fs.PrintDefaults()
	}
	if err := fs.Parse(args); err != nil {
		return err
	}
	if fs.NArg() != 1 {
		fs.Usage()
		return fmt.Errorf("expected exactly one recording, got %d", fs.NArg())
	}
	src := fs.Arg(0)

	info, err := os.Stat(src)
	if err != nil {
		return err
	}
	if *recorded == "" {
		*recorded = info.ModTime().Format("2006-01-02")
	}

	pr, err := probe(*ffprobeBin, src)
	if err != nil {
		return err
	}

	n := *number
	if n == 0 {
		if n, err = nextNumber(d.episodes); err != nil {
			return err
		}
	}

	ep := &Episode{
		Number:       n,
		Recorded:     *recorded,
		Notes:        *notes,
		EpisodeTitle: *episodeTitle,
		// add takes one source, which is the common case. An episode built
		// around several texts gets the rest added to its JSON by hand — see
		// observations(1). Zipping repeated flags into a list positionally
		// would silently misalign the moment one source has a --source-site
		// and another does not.
		Sources: []Source{{
			Title:     *title,
			URL:       *url,
			Author:    *author,
			Published: *published,
			Site:      *site,
			Note:      *note,
		}},
		Audio: Audio{
			File:     fmt.Sprintf("%03d%s", n, strings.ToLower(filepath.Ext(src))),
			Bytes:    pr.Bytes,
			Duration: pr.DurationMs,
			MIME:     mimeForExt(filepath.Ext(src)),
		},
	}
	if err := ep.validate(); err != nil {
		return err
	}
	if _, err := os.Stat(episodePath(d.episodes, n)); err == nil {
		return fmt.Errorf("episode %03d already exists (pass -number to override)", n)
	}

	// The transcript exists only inside this file, and every ordinary audio
	// operation drops it silently. If it is not here, the file has most
	// likely already been through one, so refuse rather than publish an
	// episode whose transcript is gone for good. See observations(7).
	switch {
	case pr.TranscriptStream >= 0:
		raw, err := extractStream(*ffmpegBin, src, pr.TranscriptStream)
		if err != nil {
			return err
		}
		tr, err := DecodeTranscript(raw)
		if err != nil {
			return fmt.Errorf("decoding transcript: %w", err)
		}
		ep.Transcript = tr
		fmt.Fprintf(os.Stderr, "transcript: %d words in %d segments (stream %d)\n",
			tr.WordCount(), len(tr.Segments), pr.TranscriptStream)
	case *noTranscript:
		fmt.Fprintln(os.Stderr, "transcript: none (--no-transcript)")
	default:
		var found string
		if len(pr.DataMIMEs) > 0 {
			var ms []string
			for _, m := range pr.DataMIMEs {
				ms = append(ms, m)
			}
			found = "; data tracks present: " + strings.Join(ms, ", ")
		}
		return fmt.Errorf(
			"%s carries no transcript track%s\n"+
				"        A recording that has been transcoded, remuxed or edited has lost it,\n"+
				"        and it cannot be recovered. Use the original file from the recorder,\n"+
				"        or pass --no-transcript if this recording never had one.", src, found)
	}

	if err := copyFile(src, filepath.Join(d.audio, ep.Audio.File)); err != nil {
		return err
	}
	if err := writeEpisode(d.episodes, ep); err != nil {
		return err
	}

	fmt.Printf("%s  %s\n", ep.Slug(), ep.Title())
	fmt.Printf("  audio      %s (%s, %s)\n", filepath.Join(d.audio, ep.Audio.File), ep.DurationHMS(), humanBytes(ep.Audio.Bytes))
	fmt.Printf("  metadata   %s\n", episodePath(d.episodes, ep.Number))
	if d.out != "" {
		return render(&d)
	}
	fmt.Println("\nNext: observations render -out …, then commit and push.")
	return nil
}

func cmdRender(args []string) error {
	fs := flag.NewFlagSet("render", flag.ExitOnError)
	var d dirs
	d.flags(fs)
	if err := fs.Parse(args); err != nil {
		return err
	}
	if d.out == "" {
		return fmt.Errorf("-out is required")
	}
	return render(&d)
}

func cmdList(args []string) error {
	fs := flag.NewFlagSet("list", flag.ExitOnError)
	var d dirs
	d.flags(fs)
	if err := fs.Parse(args); err != nil {
		return err
	}
	eps, err := loadEpisodes(d.episodes)
	if err != nil {
		return err
	}
	if len(eps) == 0 {
		fmt.Fprintln(os.Stderr, "no episodes yet")
		return nil
	}
	for _, e := range eps {
		words := 0
		if e.Transcript != nil {
			words = e.Transcript.WordCount()
		}
		fmt.Printf("%s  %-10s %8s  %6d words  %s\n",
			e.Slug(), e.Recorded, e.DurationHMS(), words, e.Subject())
	}
	return nil
}

func mimeForExt(ext string) string {
	switch strings.ToLower(ext) {
	case ".m4a", ".mp4", ".m4b":
		// audio/mp4 rather than the legacy audio/x-m4a: it is the registered
		// type, and podcast clients match on it.
		return "audio/mp4"
	case ".mp3":
		return "audio/mpeg"
	case ".ogg", ".opus":
		return "audio/ogg"
	case ".wav":
		return "audio/wav"
	default:
		return "application/octet-stream"
	}
}

func humanBytes(n int64) string {
	switch {
	case n >= 1<<20:
		return fmt.Sprintf("%.1f MB", float64(n)/(1<<20))
	case n >= 1<<10:
		return fmt.Sprintf("%.1f kB", float64(n)/(1<<10))
	default:
		return fmt.Sprintf("%d B", n)
	}
}

func copyFile(src, dst string) error {
	if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
		return err
	}
	in, err := os.Open(src)
	if err != nil {
		return err
	}
	defer in.Close()

	tmp := dst + ".tmp"
	out, err := os.Create(tmp)
	if err != nil {
		return err
	}
	if _, err := io.Copy(out, in); err != nil {
		out.Close()
		os.Remove(tmp)
		return err
	}
	if err := out.Close(); err != nil {
		os.Remove(tmp)
		return err
	}
	return os.Rename(tmp, dst)
}