Skip to content

Commit

Permalink
add ffmpeg compressing
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman SHamagin committed Aug 12, 2023
1 parent a32cb82 commit 152c8d8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ COPY . .
RUN GO111MODULE=on CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o iptv-proxy .

FROM alpine:3

RUN apk add --no-cache ffmpeg

COPY --from=0 /go/src/github.com/romaxa55/iptv-proxy/iptv-proxy /
ENTRYPOINT ["/iptv-proxy"]
24 changes: 19 additions & 5 deletions pkg/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ import (
"bytes"
_ "embed"
"fmt"
"github.com/canhlinh/hlsdl"
"github.com/gin-gonic/gin"
"github.com/grafov/m3u8"
"io"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"strings"
"time"

"github.com/canhlinh/hlsdl"
"github.com/gin-gonic/gin"
)

//go:embed fake.ts
Expand Down Expand Up @@ -114,7 +114,21 @@ func (c *Config) m3u8ReverseProxy(ctx *gin.Context) {

filepath, err := downloader.Download()
if err != nil {
panic(err)
log.Fatalf("Ошибка при загрузке: %v", err)
}

// Добавление суффикса к имени файла
dir := filepath[:strings.LastIndex(filepath, "/")]
base := filepath[strings.LastIndex(filepath, "/")+1:]
ext := base[strings.LastIndex(base, "."):]
newName := base[:len(base)-len(ext)] + "_compressed" + ext
outputFile := dir + "/" + newName

// Сжатие файла с помощью ffmpeg
cmd := exec.Command("ffmpeg", "-i", filepath, "-b:v", "800k", "-b:a", "128k", "-preset", "ultrafast", outputFile)
err = cmd.Run()
if err != nil {
log.Fatalf("Ошибка при сжатии файла: %v", err)
}

if listType == m3u8.MEDIA {
Expand All @@ -123,7 +137,7 @@ func (c *Config) m3u8ReverseProxy(ctx *gin.Context) {
// Замените сегменты на свои
for _, segment := range mediaList.Segments {
if segment != nil {
segment.URI = "/" + filepath // Замените на ваш путь к сегменту
segment.URI = "/" + outputFile // Замените на ваш путь к сегменту
}
}

Expand Down

0 comments on commit 152c8d8

Please sign in to comment.