Skip to content

Commit

Permalink
mega-feat: Implemented audio and video shrink
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitst05 committed Apr 30, 2024
1 parent 8b4ae37 commit 56f5461
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 30 deletions.
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ RUN chmod +x /usr/local/bin/tailwindcss
# Compile TailwindCSS styles
RUN tailwindcss -i ./web/static/index.css -o ./dist/style.css

# Install Ghostscript
# Install Ghostscript, Pandoc, and FFmpeg
RUN apt-get update && \
apt-get install -y ghostscript pandoc && \
apt-get install -y --no-install-recommends ghostscript pandoc ffmpeg && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

Expand All @@ -30,4 +30,3 @@ RUN go build -o main ./cmd/main.go
EXPOSE 3000

CMD ["./main"]

119 changes: 119 additions & 0 deletions api/handler/srhinkHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,122 @@ func HandleDOCX() http.HandlerFunc {
defer os.Remove("shrinked.docx")
}
}

func HandleMP3() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, "Could not retrieve file", http.StatusBadRequest)
return
}
defer file.Close()

w.Header().Set("Content-Type", "audio/mpeg")
w.Header().Set("Content-Disposition", `attachment; filename="shrinked.mp3"`)

cmd := exec.Command("ffmpeg", "-i", "pipe:0", "-ar", "16000", "-b:a", "32000", "-ac", "1", "-f", "mp3", "-loglevel", "quiet", "pipe:1")
cmd.Stdin = file
cmd.Stdout = w

if err := cmd.Run(); err != nil {
http.Error(w, "Could not process MP3", http.StatusInternalServerError)
return
}
}
}

func HandleWAV() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, "Could not retrieve file", http.StatusBadRequest)
return
}
defer file.Close()

w.Header().Set("Content-Type", "audio/wav")
w.Header().Set("Content-Disposition", `attachment; filename="shrinked.mp3"`)

cmd := exec.Command("ffmpeg", "-i", "pipe:0", "-ar", "16000", "-b:a", "32000", "-ac", "1", "-f", "wav", "-loglevel", "quiet", "pipe:1")
cmd.Stdin = file
cmd.Stdout = w

if err := cmd.Run(); err != nil {
http.Error(w, "Could not process WAV", http.StatusInternalServerError)
return
}
}
}

func HandleMP4() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, "Could not retrieve file", http.StatusBadRequest)
return
}
defer file.Close()

tempFile, err := os.CreateTemp("", "original-*.mp4")
if err != nil {
http.Error(w, "Could not create temporary file", http.StatusInternalServerError)
return
}
defer os.Remove(tempFile.Name())

_, err = io.Copy(tempFile, file)
if err != nil {
http.Error(w, "Could not save uploaded file", http.StatusInternalServerError)
return
}
tempFile.Close()

outputFile := "shrinked.mp4"
cmd := exec.Command("ffmpeg", "-i", tempFile.Name(), "-c:v", "libx265", "-preset", "veryfast", "-tag:v", "hvc1", "-b:v", "800k", "-bufsize", "1200k", "-vf", "scale=1080:1920,format=yuv420p", "-b:a", "128k", outputFile)

if err := cmd.Run(); err != nil {
http.Error(w, "Could not process MP4", http.StatusInternalServerError)
return
}

processedFile, err := os.Open(outputFile)
if err != nil {
http.Error(w, "Could not open processed file", http.StatusInternalServerError)
return
}
defer processedFile.Close()

w.Header().Set("Content-Type", "video/mp4")
w.Header().Set("Content-Disposition", `attachment; filename="shrinked.mp4"`)

_, err = io.Copy(w, processedFile)
if err != nil {
http.Error(w, "Could not write processed file", http.StatusInternalServerError)
return
}
defer os.Remove("shrinked.mp4")
}
}

func HandleMKV() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, "Could not retrieve file", http.StatusBadRequest)
return
}
defer file.Close()

w.Header().Set("Content-Type", "video/x-matroska")
w.Header().Set("Content-Disposition", `attachment; filename="shrinked.mkv"`)

cmd := exec.Command("ffmpeg", "-i", "pipe:0", "-c:v", "libx265", "-preset", "veryfast", "-tag:v", "hvc1", "-b:v", "800k", "-bufsize", "1200k", "-vf", "scale=1080:1920,format=yuv420p", "-b:a", "128k", "-f", "matroska", "-loglevel", "quiet", "pipe:1")
cmd.Stdin = file
cmd.Stdout = w

if err := cmd.Run(); err != nil {
http.Error(w, "Could not process MKV", http.StatusInternalServerError)
return
}
}
}
4 changes: 4 additions & 0 deletions internal/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func (s *Server) RegisterRoutes() http.Handler {
router.HandleFunc("/shrink/jpg", handler.HandleJPG())
router.HandleFunc("/shrink/pdf", handler.HandlePDF())
router.HandleFunc("/shrink/docx", handler.HandleDOCX())
router.HandleFunc("/shrink/mp3", handler.HandleMP3())
router.HandleFunc("/shrink/wav", handler.HandleWAV())
router.HandleFunc("/shrink/mp4", handler.HandleMP4())
router.HandleFunc("/shrink/mkv", handler.HandleMKV())

return router
}
2 changes: 1 addition & 1 deletion web/templates/components/audio_svg_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions web/templates/components/cards.templ
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ templ Cards(choice string, cards []string) {
} else if choice == "audio" {
<li
class="flex flex-col items-center p-8 rounded-lg hover:scale-110 bg-[#7AA2E3] cursor-pointer"
onclick="document.getElementById('pngFile').click();"
onclick="document.getElementById('mp3File').click();"
>
@audioSVG()
<p class="text-[#f8f6e3] font-bold text-4xl">MP3</p>
<input type="file" accept="audio/mp3" id="mp3File" name="file" onchange="submitForm('mp3File')" hidden/>
<input type="file" accept="audio/mpeg" id="mp3File" name="file" onchange="submitForm('mp3File')" hidden/>
</li>
<li
class="flex flex-col items-center p-8 rounded-lg hover:scale-110 bg-[#7AA2E3] cursor-pointer"
Expand All @@ -74,7 +74,7 @@ templ Cards(choice string, cards []string) {
>
@videoSVG()
<p class="text-[#f8f6e3] font-bold text-4xl">MKV</p>
<input type="file" accept="video/mkv" id="mkvFile" name="file" onchange="submitForm('mkvFile')" hidden/>
<input type="file" accept="video/x-matroska" id="mkvFile" name="file" onchange="submitForm('mkvFile')" hidden/>
</li>
}
<script>
Expand All @@ -92,6 +92,14 @@ templ Cards(choice string, cards []string) {
form.action = '/shrink/pdf'
} else if (fileInputId === 'docxFile') {
form.action = '/shrink/docx';
} else if (fileInputId === 'mp3File') {
form.action = '/shrink/mp3'
} else if (fileInputId === 'wavFile') {
form.action = '/shrink/wav'
} else if (fileInputId === 'mp4File') {
form.action = '/shrink/mp4'
} else if (fileInputId === 'mkvFile') {
form.action = '/shrink/mkv'
}

var fileInput = document.getElementById(fileInputId);
Expand Down
26 changes: 13 additions & 13 deletions web/templates/components/cards_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion web/templates/components/document_svg_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 56f5461

Please sign in to comment.