thisago / kiwifyDownload

Downloads the kiwify videos from course JSON
MIT License
7 stars 1 forks source link

Corrupted files #3

Open VctorMatheus opened 2 months ago

VctorMatheus commented 2 months ago

After a few minutes trying to understand about Json, and a few hours about Nim, it worked!

The app looks like running perfectly, the problem is on the downloaded files: The video file is always corrupted in every lesson of every module. Some are .MOV and ohters .MP4, but all of them are 0KB.

While watching the app run, i've spotted something that maybe helps to resolve the issue i'm having, so i'm gonna leave a print down here.

i'm on my first year of computer science degree, so i'm very unexperient in almost everything that is happening on the app, but it would be very helpful to me on my job, so i'm gonna wait hopefully for a reply that solves my case. imagem_2024-06-25_230608729

katomaro commented 1 month ago

@VctorMatheus I don't know what nim is (someone just linked this repository to me as I am about to re-implement my own scrapper and I was talking about their endpoints, which turns out there are none defined on this repository so not quite helpful) but the error you are facing comes exactly from this line:
https://github.com/thisago/kiwifyDownload/blob/13da9d5ffdf5f97d6acdc94b33450965e78ea7a3/src/kiwifyDownload.nim#L18
if you check the output for FFMpeg on your terminal it errors out at the file name, so the string formatting is giving you invalid strings. Helping you understand it:
-c copy tells FFMpeg that it should copy the codecs from the input file (-i argument before), which should be an URL contained within the JSON into the output file (which is what comes right after this argument, so "{tmpDest}" (mind the quotation marks here as this is done to avoid invalid OS paths)). This is my caveat as I don't know nim, so while it is clear that is a loop, I am unsure what the input is (and can't be as bothered to figure out), but you can cut that line at right before the then markers (&&), it seems like the author is using FFMpeg to download the stream segments and let it handle the merging later and then have the terminal run the mv command to move the final file from tempDest to dest (both should be paths).

If the code is functional, you can just change the output parameter from tempDest to dest and forget everything about moving files afterwards, if it isn't (as this assume proper path handling), you can cut the line as suggested and start another command to explicitly move the file after the download loop, something like the following should work:

cmd = fmt"""ffmpeg -protocol_whitelist file,http,https,tcp,tls -allowed_extensions ALL -i {url} -bsf:a aac_adtstoasc -c copy "{tmpDest}""""
    down = startProcess(
      cmd,
      options = {poStdErrToStdOut, poUsePath, poEvalCommand, poDaemon}
    )
  for line in down.lines:
    echoSingleLine line
  echo ""
  close down
mvCmd = fmt"""mv "{tmpDest}" "{dest}""""
mvProc = startProcess(mvCmd, options = {poStdErrToStdOut, poUsePath, poEvalCommand, poDaemon})
close mvProc

nowhere near as optimal but likely works, one quick reminder that mv isn't a terminal (cmd) command on Windows, the equivalent would be ren (but there's a few things to be noted, like not being able to create new paths)