Extracting from Youtube on Ubuntu

How to extract a video from Youtube and optionally to extract mp3 from the video. On Ubuntu 20.10:

Install youtube-dl (for downloading from youtube):

sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl

Install ffmpeg for sound extraction:

sudo apt install ffmpeg

How to extract video (note – has to be your paths to python3 and youtube-dl):

/usr/bin/python3 /usr/local/bin/youtube-dl https://www.youtube.com/watch?<youtubeid>

How to extract sound:

ffmpeg -i '<title>-<youtubeid>.mkv' '<title>.mp3'

With thanks to 3 Easy Ways to Download YouTube Videos in Ubuntu and Other Linux Distributions; How to Extract Audio From Video in Ubuntu and Other Linux Distributions; and Youtube-dl: Python not found (18.04)

Trouble copying audio file – until rdd-copy

I had trouble copying a audio file (wav) from a CD to my computer – the copy process was always getting stuck at exactly the same point 153MB in. And it didn’t matter whether I was using sound-juicer or nautilus. The answer was to install rdd (sudo apt-get install rdd). rdd copes with errors by supplying blanks (assuming multiple careful attempts to read the data have all failed) rather than halting.

The required command was rdd-copy src dest. But what to supply as src? I tried /media and similar but no luck. The final answer was ‘/run/user/1000/gvfs/cdda:host=sr0/Track 3.wav’. But how to find it? http://askubuntu.com/questions/222622/cdrom-is-mounted-but-where. Just drag the file from nautilus to the terminal and see what is displayed there. The following worked even though it took a long time to get the file:

rdd-copy '/run/user/1000/gvfs/cdda:host=sr0/Track 3.wav' /home/g/Desktop/track3.wav

Rotate video (and cope with frame rate problem)

The problem – I had taken a video on a smart phone upside down and needed to rotate it (Note – rotating is not the same as flipping). Here is the successful command I used on Ubuntu:

avconv -i "my_input_video.mp4" -r 25 -vf transpose=1,transpose=1 my_output_video.mp4

Note – I needed to rotate it so chaining two 90 degree transpose commands (transpose=1 is a single 90 degree transposition) worked (see How can I rotate video by 180 degrees with avconv).

Note the -r 25. I don’t pretend to understand all the options of ffmpeg/avconv but the -r 25 forced the conversion to use a manageable number of frames. Without it, converting one particular video was failing, thrashing the CPU, and taking forever. There was a message in the ffmpeg/avconv output about MB rate > level limit. Another video worked fine without -r 25 but if you have the same problem with MB rate give -r 25 a try. Or better yet, learn more about what you’re doing ;-). I didn’t have the time or inclination so tried to do the least I could to get a good result.

Update – needed to alter aspect ratio as per:

avconv -i "my_image.MOD" -aspect 0.487 -r 25 -vf transpose=1,transpose=1,transpose=1 -strict -2 "my_image.mp4"

Quick MPG4 to AVI conversion

The problem – my wife had an mpg4 but it wouldn’t play on the machine upstair. Needed an AVI instead. I couldn’t be bothered learning all the various settings of ffmpeg (libav is a fork) but found an acceptable (albeit inefficient) way of getting the same quality in the AVI as in the MPG4. OK, the file size was 3-4x larger, so I would recommend optimising things if doing multiple conversions, but this is a good and simple answer for the occasional one-off.

ffmpeg -i original.mp4 -sameq converted_version.avi