Initial commit

This commit is contained in:
Arkaprabha Chakraborty
2024-08-08 01:55:38 +05:30
commit 7316ba17a1
30 changed files with 20519 additions and 0 deletions

36
backend/.gitignore vendored Normal file
View File

@@ -0,0 +1,36 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
*.test
*.out
# Test binary, build output, and coverage files
*.test
*.out
*.cover
*.prof
*.fuzz
# Vendor directory
/vendor/
# IDE/editor-specific files
.idea/
.vscode/
*.sublime-project
*.sublime-workspace
# GoLand files
*.iml
*.ipr
*.iws
# Log files
*.log
# OS-specific files
.DS_Store
Thumbs.db

42
backend/Dockerfile Normal file
View File

@@ -0,0 +1,42 @@
# Use the official Golang image
FROM golang:1.20-alpine
# Set the Current Working Directory inside the container
WORKDIR /server
# Copy go mod and sum files
COPY go.mod go.sum ./
# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download
# Copy the source code into the container
COPY . .
# Build the Go app
RUN go build -o main .
RUN apk add --no-cache \
python3 \
python3-dev \
py3-pip \
build-base \
git \
wget \
ffmpeg
# Create a virtual environment for Python
RUN python3 -m venv /venv
# Activate the virtual environment and install yt-dlp
RUN /venv/bin/pip install --upgrade pip && \
/venv/bin/pip install yt-dlp
# Ensure ffmpeg is in the PATH
ENV PATH="/usr/bin:/venv/bin:${PATH}"
# Expose port 8080 to the outside world
EXPOSE 8080
# Command to run the executable
CMD ["./main"]

20
backend/go.mod Normal file
View File

@@ -0,0 +1,20 @@
module DownLink
go 1.20
require github.com/labstack/echo v3.3.10+incompatible
require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
)

35
backend/go.sum Normal file
View File

@@ -0,0 +1,35 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

137
backend/main.go Normal file
View File

@@ -0,0 +1,137 @@
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/google/uuid"
)
// VideoDownloadRequest represents the request structure for video download
type VideoDownloadRequest struct {
URL string `json:"url"`
Quality string `json:"quality"`
}
// getBestFormat fetches and returns the best format for the given quality
func getBestFormats(url string, targetResolution string) (string, string, error) {
log.Printf("Getting best formats for URL: %s, target resolution: %s", url, targetResolution)
cmd := exec.Command("yt-dlp", "--list-formats", url)
output, err := cmd.CombinedOutput()
if err != nil {
return "", "", fmt.Errorf("failed to list formats: %v\nOutput: %s", err, string(output))
}
lines := strings.Split(strings.TrimSpace(string(output)), "\n")
var bestVideoFormat, bestAudioFormat string
targetHeight := strings.TrimSuffix(targetResolution, "p")
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) < 3 {
continue
}
if strings.Contains(line, targetHeight) && strings.Contains(line, "mp4") && bestVideoFormat == "" {
bestVideoFormat = fields[0]
}
if strings.Contains(line, "audio only") && strings.Contains(line, "m4a") && bestAudioFormat == "" {
bestAudioFormat = fields[0]
}
if bestVideoFormat != "" && bestAudioFormat != "" {
break
}
}
if bestVideoFormat == "" || bestAudioFormat == "" {
return "", "", fmt.Errorf("no suitable formats found for resolution: %s", targetResolution)
}
log.Printf("Selected video format: %s, audio format: %s", bestVideoFormat, bestAudioFormat)
return bestVideoFormat, bestAudioFormat, nil
}
func downloadVideo(c echo.Context) error {
req := new(VideoDownloadRequest)
if err := c.Bind(req); err != nil {
return err
}
if req.URL == "" || req.Quality == "" {
return echo.NewHTTPError(http.StatusBadRequest, "URL and Quality are required")
}
// Get the best video and audio formats
videoFormat, audioFormat, err := getBestFormats(req.URL, req.Quality)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get best formats: %v", err))
}
// Create a temporary directory for downloading files
tmpDir, err := os.MkdirTemp("", "downlink")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create temporary directory: %v", err))
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
log.Printf("Failed to clean up temporary directory: %v", err)
}
}()
uid := uuid.New().String()
videoPath := filepath.Join(tmpDir, fmt.Sprintf("video_%s.mp4", uid))
audioPath := filepath.Join(tmpDir, fmt.Sprintf("audio_%s.m4a", uid))
outputPath := filepath.Join(tmpDir, fmt.Sprintf("output_%s.mp4", uid))
// Download video
cmdVideo := exec.Command("yt-dlp", "-f", videoFormat, "-o", videoPath, req.URL)
if err := cmdVideo.Run(); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to download video: %v", err))
}
// Download audio
cmdAudio := exec.Command("yt-dlp", "-f", audioFormat, "-o", audioPath, req.URL)
if err := cmdAudio.Run(); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to download audio: %v", err))
}
// Combine video and audio using ffmpeg
cmdMerge := exec.Command("ffmpeg", "-i", videoPath, "-i", audioPath, "-c", "copy", outputPath)
if err := cmdMerge.Run(); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to merge video and audio: %v", err))
}
// Serve the file with appropriate headers
return c.Attachment(outputPath, fmt.Sprintf("video_%s.mp4", uid))
}
func main() {
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete},
}))
// Routes
e.GET("/downlink/", func(c echo.Context) error {
return c.String(http.StatusOK, "Backend for DownLink is running.\n")
})
e.POST("/downlink/download", downloadVideo)
// Start server
e.Logger.Fatal(e.Start(":8080"))
}