HowTo download Coub via simple script and loop it (Linux way)

If you need to download coub video without any services, then this article is what you need.

Before to begin, make sure you have both ffmpeg and youtube-dl are installed on your system. I already have an article about the possibilities of youtube-dl and how to use it.

Here youtube-dl need to download video and audio streams of coub. FFmpeg needed to combine video and audio stream of coub together and loop it. You can choose the number of loops by yourself.

Here is the script itself

#!/usr/bin/env bash
set -e
youtube-dl -o video.mp4 "$1"
youtube-dl -f html5-audio-high -o audio.mp3 "$1"
printf '\x00\x00' | dd of=video.mp4 bs=1 count=2 conv=notrunc
read -p "count of loops: " loops
read -p "filename: " name
for i in `seq 1 "$loops"`; do echo "file 'video.mp4'" >> loop.txt; done
ffmpeg -hide_banner -f concat -i loop.txt -i audio.mp3 -c copy -shortest -movflags faststart "$name".mp4
rm video.mp4 audio.mp3 loop.txt

Save this code to a file. Make it executable. Suppose we named the file as coub. Make coub executable.

chmod +x ./coub

You can use the script like this

coub "link to coub video"

In the process, it will ask you how many loops you need and how to name the file. Coub will download the file to the current directory.

If you want to make it accessible from any directory, like any utility in Linux, then move it

mv ./coub /usr/bin/

How it works

Coub resource stores video and audio tracks separately. They are played synchronously in the media player. Therefore, we need to download separately both video and audio and then link them. Therefore, we use youtube-dl for downloading, and ffmpeg for linking.

In ffmpeg we use the “concat” option to loop the video. This option simply links together the video tracks, without re-encoding, so the video quality remains high and untouched.

But what does this command do?

printf '\x00\x00' | dd of=video.mp4 bs=1 count=2 conv=notrunc

The video file from the Coub site downloads with a strange signature in the file. This signature is used by programs to determine the type of file, what standards, technology it uses. It’s like a passport.

Because of the wrong signature, ffmpeg cannot determine the file type and cannot open it.

In the video file from the Coub, the first two bytes are not null bytes.
Bytes numbered above. So, the first two bytes are not null bytes.

I searched the Internet and found that the formats .mp4, .mov, .flv, m4v, .3gp have three null bytes at the beginning of the file. So, the dd utility can write bytes to files, so we use it here.

dd utility was used, first three bytes are null bytes
Now that’s correct. This is another file on which the dd utility was used. So, ffmpeg now can open it

But you can also use the hex editor if you want to do it manually.

Now that’s all.