How to Cut and Download Youtube Videos using Command Line


In some of my free time, I am actively using Instagram, not to show off or seeing people’s life that I don’t even care about. For me, this social media is perfect for everyday reminders for positive feeds. Usually, people choose a youtube video at the best moment, then re-post it on Instagram as a short one-minute clip, and sometimes I do so.

Well, the process of downloading, cutting, then re-posting it on Instagram is easy but not a quick task, especially if you do it on your phone. Then I thought it would be better for us (people who loved to share short clips from youtube) if we can do that in just one step. But first, let’s check what are the tools available for this task.

In Search of Youtube Downloader

So I did a little search, and then found a popular youtube downloader repository on Github, youtube-dl. Unfortunately, it is not a mobile app, it’s made for PC, works on Linux, Mac, and Windows. It works well and is easy enough for me to download the youtube videos that I want. As simple as:

$ youtube-dl https://www.youtube.com/watch?v=4B9hgn-GG3c

And let the youtube-dl handle the rest.

Cut on The Best Moment

So now we have our video. Let’s cut it at the best moment that we want. For this, I found that FFmpeg has easy cut commands (source). Make sure it is already installed on your system.

$ ffmpeg -i source.mp4 -ss 00:00:05 -t 00:00:10 -c copy cut_video.mp4

The -i param is for input video, -s is for the starting point to cut, -t is for the length, and -c is for the output. Having these tools, we are able to combine and make our automation script to download and cut. However, there’s still a problem with this approach.

Another Problem

Most of the time, the clip that we want is short. The problem appears when we need a small clip from super-duper HD really long video. Download time will increase. It’s really wasteful if you just need a small part of it. Then, what can we do?

Solution

By the time I am writing this post, apparently, Youtube is capping download speed on any downloader including the youtube-dl to around 80KBps.

Then I search around and found another downloader. Meet yt-dlp which is the fork of youtube-dl, it contains various fixes and additional features. What surprised me is that it can download only part of the interval using ffmpeg using this command:

$ yt-dlp --downloader ffmpeg --downloader-args "ffmpeg_i:-ss 00:41:21.00 -to 00:41:31.00" --no-check-certificate https://www.youtube.com/watch?v=dqvPzbO__bQ&ab_channel=RodjaTV

Below is me downloading a part of a 57 minutes video only from minutes 41:21 to 41:31 is done in seconds, awesome! No need to download the whole part of the videos.

ibndias@xps15:~$ yt-dlp --downloader ffmpeg --downloader-args "ffmpeg_i:-ss 00:41:21.00 -to 00:41:31.00" --no-check-certificate https://www.youtube.com/watch?v=dqvPzbO__bQ&ab_channel=RodjaTV
[1] 197204
ibndias@xps15:~$ [youtube] dqvPzbO__bQ: Downloading webpage
[youtube] dqvPzbO__bQ: Downloading android player API JSON
[info] dqvPzbO__bQ: Downloading 1 format(s): 248+251
[download] Destination: Shahihu Fiqhis Sunnah  - Syarat-Syarat Tayamum l Ustadz Dr. Musyaffa Ad Dariny [dqvPzbO__bQ].webm
Input #0, matroska,webm, from 'https://r5---sn-n3cgv5qc5oq-20nz.googlevideo.com/videoplayback?expire=1634250846&ei=_ltoYb_xNMTDgQPTpIWgAQ&ip=58.235.241.85&id=o-AGmMeQgP3Rc7RQQIxOyzavFZY0VoTpQPlIAy4msJnQLT&itag=248&source=youtube&requiressl=yes&mh=ax&mm=31%2C29&mn=sn-n3cgv5qc5oq-20nz%2Csn-n3cgv5qc5oq-bh2ld&ms=au%2Crdu&mv=m&mvi=5&pl=24&initcwndbps=1661250&vprv=1&mime=video%2Fwebm&gir=yes&clen=656659706&dur=3468.400&lmt=1633920085252967&mt=1634228955&fvip=5&keepalive=yes&fexp=24001373%2C24007246&c=ANDROID&txp=6316224&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRQIgMLOzOcvQTl1k8pFRLmFf8RvFUDA0hPOmsmwjFjYWhhACIQD-kl5de2iWr9hNdXWGHDlm4FP3AoTDTtJy0iMj9DH1SQ%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRAIgcKqqM66NWuaPnvugVRSxPjREFIHn35fQjUqD7EBRoX8CIA5n85It0dz3f_kFlxNkkGXCJHIQG1BBqKBaf6piKLO8':
  Metadata:
    encoder         : google/video-file
  Duration: 00:57:48.40, start: 0.000000, bitrate: 1514 kb/s
    Stream #0:0(eng): Video: vp9 (Profile 0), yuv420p(tv, bt709), 1920x1080, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 1k tbn, 1k tbc (default)
Input #1, matroska,webm, from 'https://r5---sn-n3cgv5qc5oq-20nz.googlevideo.com/videoplayback?expire=1634250846&ei=_ltoYb_xNMTDgQPTpIWgAQ&ip=58.235.241.85&id=o-AGmMeQgP3Rc7RQQIxOyzavFZY0VoTpQPlIAy4msJnQLT&itag=251&source=youtube&requiressl=yes&mh=ax&mm=31%2C29&mn=sn-n3cgv5qc5oq-20nz%2Csn-n3cgv5qc5oq-bh2ld&ms=au%2Crdu&mv=m&mvi=5&pl=24&initcwndbps=1661250&vprv=1&mime=audio%2Fwebm&gir=yes&clen=46413793&dur=3468.441&lmt=1633920481569809&mt=1634228955&fvip=5&keepalive=yes&fexp=24001373%2C24007246&c=ANDROID&txp=6311224&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRAIgTK9mvpNA_MmCt6AvGdq3dizvvldX0BnMKQQ8SSh3vS8CIGhhnwqWzfa8bFDCiYjduCQrKGRhYkRUB4AAqhtRWUfI&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRAIgcKqqM66NWuaPnvugVRSxPjREFIHn35fQjUqD7EBRoX8CIA5n85It0dz3f_kFlxNkkGXCJHIQG1BBqKBaf6piKLO8':
  Metadata:
    encoder         : google/video-file
  Duration: 00:57:48.44, start: -0.007000, bitrate: 107 kb/s
    Stream #1:0(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
Output #0, webm, to 'file:Shahihu Fiqhis Sunnah  - Syarat-Syarat Tayamum l Ustadz Dr. Musyaffa Ad Dariny [dqvPzbO__bQ].webm.part':
  Metadata:
    encoder         : Lavf58.45.100
    Stream #0:0(eng): Video: vp9 (Profile 0), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 25 fps, 25 tbr, 1k tbn, 1k tbc (default)
    Stream #0:1(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #1:0 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=  275 fps=0.0 q=-1.0 Lsize=    2414kB time=00:00:09.98 bitrate=1981.2kbits/s speed=23.7x    
video:2257kB audio:150kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.263868%
[ffmpeg] Downloaded 2471757 bytes
[download] 100% of 2.36MiB in 00:06
ibndias@xps15:~$ 

Now that we have this quick way to download part of youtube videos, I am wondering if we can have this on mobile apps or web apps.

I personally prefer web apps, whether it is through a VPS or maybe even just a pure PHP script is enough, let’s see if we can make it. ๐Ÿ™‚

Update: We Made It! Please Try! ๐Ÿ™‚

Finally, in some of my free time, I was able to create my own web service to cut the portion of youtube videos. Go to shaviq.derrylab.com to try! Please try it a lot and give us some useful feedback. You can also add some captions for your trimmed videos for videos title, but if you just want to cut the videos, just leave the title fields empty. ๐Ÿ™‚

SHAVIQ stands for Share Videos Quickly

In the future, I am planning to add some style templates for captions. It will be good for sharing personalization. See you later! ๐Ÿ™‚


Leave a Reply

Your email address will not be published. Required fields are marked *