Skip to content

Commit

Permalink
Improve ascii stream for any one symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed May 16, 2024
1 parent 83c0053 commit 6d967bc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
7 changes: 4 additions & 3 deletions internal/mjpeg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
- escape text param with urlencode
- you can stream any camera or file from a disc

**go2rtc.yaml** - transcoding to MJPEG, terminal size - 210x60, fps - 4
**go2rtc.yaml** - transcoding to MJPEG, terminal size - 210x59 (16/9), fps - 10

```yaml
streams:
macarena: ffmpeg:macarena.mp4#video=mjpeg#hardware#width=210#height=60#raw=-r 4
macarena: ffmpeg:macarena.mp4#video=mjpeg#hardware#width=210#height=59#raw=-r 10
```
**API params**
- `color` - foreground color, values: empty, `8`, `256`, `rgb`
- `back` - background color, values: empty, `8`, `256`, `rgb`
- `text` - character set, values: empty, one space, two spaces, anything you like (in order of brightness)
- `text` - character set, values: empty, one char, `block`, list of chars (in order of brightness)
- example: `%20` (space), `block` (block elements), `ox` (two chars)

**Examples**

Expand Down
35 changes: 15 additions & 20 deletions pkg/ascii/ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,35 +61,30 @@ func NewWriter(w io.Writer, foreground, background, text string) io.Writer {
}
}

var ascii string
switch text {
case "":
ascii = ` .::--~~==++**##%%$@`
case " ":
a.text = func(r, g, b uint32) {
a.buf = append(a.buf, ' ')
if len(text) == 1 {
// fast 1 symbol version
a.text = func(_, _, _ uint32) {
a.buf = append(a.buf, text[0])
}
case " ":
a.text = func(r, g, b uint32) {
a.buf = append(a.buf, ' ', ' ')
} else {
switch text {
case "":
text = ` .::--~~==++**##%%$@` // default for empty text
case "block":
text = " ░░▒▒▓▓█" // https://en.wikipedia.org/wiki/Block_Elements
}
case "block":
ascii = " ░░▒▒▓▓█" // https://en.wikipedia.org/wiki/Block_Elements
default:
ascii = text
}
if ascii != "" {
if runes := []rune(ascii); len(runes) != len(ascii) {

if runes := []rune(text); len(runes) != len(text) {
k := float32(len(runes)-1) / 255
a.text = func(r, g, b uint32) {
i := gray(r, g, b, k)
a.buf = utf8.AppendRune(a.buf, runes[i])
}
} else {
k := float32(len(ascii)-1) / 255
k := float32(len(text)-1) / 255
a.text = func(r, g, b uint32) {
i := gray(r, g, b, k)
a.buf = append(a.buf, ascii[i])
a.buf = append(a.buf, text[i])
}
}
}
Expand Down Expand Up @@ -130,7 +125,7 @@ func (a *writer) Write(p []byte) (n int, err error) {
a.buf = append(a.buf, '\n')
}

a.buf = append(a.buf, "\033[0m\n"...)
a.buf = append(a.buf, "\033[0m"...)

if n, err = a.wr.Write(a.buf); err == nil {
a.wr.(http.Flusher).Flush()
Expand Down

0 comments on commit 6d967bc

Please sign in to comment.