AlttiRi / twitter-click-and-save

[userscript] Adds buttons to save images and videos in Twitter, also does some other enhancements. (Twitter image and video downloader)
https://greasyfork.org/en/scripts/430132
GNU General Public License v3.0
102 stars 8 forks source link

After downloading and transferring to the phone, the video doesn't have any audio #37

Open CHIHOLUEN opened 9 months ago

CHIHOLUEN commented 9 months ago

Hey there, today I downloaded a video from Twitter and used LANDrop to transfer it to my phone, but it turns out the video had no audio. At first, I thought it was a LANDrop issue, but the same happened with other transfer methods. Then, I tried downloading Twitter videos using other methods and used LANDrop again to transfer them to my phone, and those videos had audio. Not sure if it's a bug, but hoping you can help resolve this. Thanks.

biggestsonicfan commented 9 months ago

So be aware that twitter's gif system encodes all gifs as MP4s, so the video you're looking at may never had audio. Can you post the link?

AlttiRi commented 9 months ago

~+1 for a link. Can't reproduce.~

AlttiRi commented 9 months ago

It's ~possibly~ Twitter's encoder's issue.

I googled Twitter-vork muxer (from MediaInfo) and I found:

Looks like they use some shitty muxer software ("Twitter-vork muxer") that produces bad-supported mp4 files.

These mp4 files work fine in Media Player Classic (MPC-HC) on Windows, but VLC plays them without sound.

For example, a remux (repacking from mp4 to mp4 without re-encoding*) created with ffmpeg works fine in VLC player:

ffmpeg.exe -i video.mp4 -c copy -map 0 -map_metadata 0 video.remux.mp4

[!NOTE] _*With ffmpeg's -c copy + -map 0 + -map_metadata 0 options._

[!TIP] BTW, additionally you can use:

So, Twitter's encoding (video and audio streams) is fine, but Twitter's mp4 container has issues.


BTW, ffmpeg prints the follow warnings while remuxing:

[mp4 @ 000001b886d78340] Non-monotonous DTS in output stream 0:0; previous: 107200, current: 107000; changing to 107201. This may result in incorrect timestamps in the output file.
[mp4 @ 000001b886d78340] Non-monotonous DTS in output stream 0:0; previous: 1655320, current: 1655120; changing to 1655321. This may result in incorrect timestamps in the output file.
[mp4 @ 000001b886d78340] Non-monotonous DTS in output stream 0:0; previous: 1691320, current: 1691120; changing to 1691321. This may result in incorrect timestamps in the output file.
...
CHIHOLUEN commented 9 months ago

So be aware that twitter's gif system encodes all gifs as MP4s, so the video you're looking at may never had audio. Can you post the link?

link:https://twitter.com/fluffiefifi/status/1734723306400752025 Like with this link, when I download it and use LANDrop to send it to my phone, the video loses its audio.

CHIHOLUEN commented 9 months ago

Thanks AlttiRi for getting back to me. Since I haven't learned programming, do I tackle this issue myself or are you going to help fix it later?

AlttiRi commented 9 months ago

Well, technically the script works as intended — it downloads the original (after Twitter's enconing) mp4 files in the best available quality, with sound, as gallery-dl does.

There is the audio stream in this mp4 file, but your device just can't play the file correctly.

On Windows and on Android it plays fine everywhere except VLC player.

BTW, as far I know, Apple devices also can't play webm files, do not support VP9 codec (in particularly, it's the reason of lack of ability to watch some videos on YouTube in 4k), have issues with mkv container.


The possible fix is to remux (repack) the downloaded video and audio streams (without re-encoding/transcoding, so the quality will be absolutely the same) from "bad" Twitter's mp4 container to a "more correct" mp4 container. For example, as noted above, with ffmpeg program.

Possibly (I need to test it), remuxing can be optionally done inside a user script with https://github.com/Kagami/ffmpeg.js, although it seems for me it's a bit out of scope for this script.

AlttiRi commented 8 months ago

Twitter have already updated the setting of its encoder, so the new mp4 files should be played correctly on Apple devices and in VLC player.

However, the files uploaded to Twitter from 2023.11.30 until 2023.12.15* will have the problematic mp4 container anyway. So, if you need to play them on an Apple phone, you need to manually remux them.

Here is a Bash command to create remux files with ffmpeg.exe for the affected files in the current folder:

ls -1 *.mp4 | \
grep -Ev '\.remux\.mp4$' | \
grep -E '^\[twitter\].+2023\.(12\.(0[0-9]|1[0-4])|11\.30)' | \
while read file; do 
  output="${file%.mp4}.remux.mp4";
  ffmpeg.exe \
    -i "$file" `# input file` \
    -c copy    `# do not re-encode` \
    -map 0     `# copy all streams` \
    -map_metadata 0     `# save all metadata` \
    -fflags bitexact    `# do not add the encoder name metadata` \
    -movflags faststart `# web optimisation` \
    -loglevel error `# do not print logs` \
    -n              `# do not overwrite already created .remux.mp4` \
    "$output"       `# output file` \
      < /dev/null;  `# to close the stdin of the ffmpeg`
  echo "$output";
done;

It selects all .mp4 files that start with "[twitter]" text except .remux.mp4 (already remuxed mp4), then filters them to select only affected files (with date from 2023.11.30 to 2023.12.14) and creates remuxes of them with ffmpeg.exe.


* from ~"UTC 2023-11-30 15:00:00" to ~"UTC 2023-12-14 22:00:00"


Compact version:

ls -1 *.mp4 | grep -Ev '\.remux\.mp4$' | \
grep -E '^\[twitter\].+2023\.(12\.(0[0-9]|1[0-4])|11\.30)' | \
while read file; do 
  output="${file%.mp4}.remux.mp4";
  ffmpeg.exe -i "$file" -c copy -map 0 -map_metadata 0 -fflags bitexact -movflags faststart -loglevel error -n "$output" < /dev/null;
  echo "$output";
done;

Without the second row it will work for every .mp4 file in a folder.

CHIHOLUEN commented 8 months ago

AlttiRi

Hi AlttiRi, thanks a bunch for the update! After downloading it from the computer and transferring it to my iPhone using LANDrop, the video's audio is working fine now. Appreciate it!