Showing posts with label Video. Show all posts
Showing posts with label Video. Show all posts

Sunday, 6 January 2019

Downloading YouTube videos with hardcoded subtitles

I recently bought a Chromecast device, and I use VLC in my Linux computer to stream videos to our TV. Though not always, sometimes these videos come from YouTube, and I would like to be able to see the video subtitles in the TV, but as the time of writing this, VLC+Chromecast and subtitles don't seem to play along nicely (see https://code.videolan.org/videolan/vlc-android/issues/502). While a future version of VLC is likely to include this feature, in the meantime I figured out the way to hardcode the subtitles. It is very easy, but time consuming, since we have to encode the video again. The following commands are an example of what needs to be done:

(useful to see the different available formats, and get the one that corresponds to 1280x720)
youtube-dl  --list-formats  https://www.youtube.com/watch?v=AW138ZTKioM

(and the available subtitles)
youtube-dl  --list-subs https://www.youtube.com/watch?v=AW138ZTKioM
 
(download the video in the required format, -f, and convert the subtitles to the .srt format)
youtube-dl  -f 22  --convert-subs srt --write-sub --sub-lang en https://www.youtube.com/watch?v=AW138ZTKioM
 
(use ffmpeg to reencode with the downloaded subtitles hardcoded)
ffmpeg -i The\ Anthropocene\ -\ The\ age\ of\ mankind\ -\ Docu\ -\ 2017-AW138ZTKioM.mp4 -vf "subtitles='The Anthropocene - The age of mankind - Docu - 2017-AW138ZTKioM.en.srt'" The_age_of_mankind.mp4

Done!

[update: 8/1/2019. Hardcoding the subtitles might be handy in several situations, but for watching YouTube videos via Chromecast, it is actually much easier to use Chromium (I'm using ArchLinux where Chrome is not in the official repositories), which comes with the option of casting YouTube, a given tab, or even the whole desktop).

Monday, 29 June 2015

Stabilizing and slowing down videos

Those of us with a regular camera and no tripod are used to shaky videos. To fight this disease, we can use software to help stabilizing it. In my current system, Ubuntu 14.04, the software 'transcode' already has everything you need, and it is extremely easy to get it working. Assuming you have a shaky video, just issue the following two commands:

transcode -J stabilize -i ORIGINAL.MOV
transcode -J transform -i ORIGINAL.MOV -y xvid -o STABILIZED.MOV

The result can be pretty good. As a comparison, see below (left: original; right: after stabilizing).



Another nice thing to be able to do with videos is to slow them down in certain parts, but creating interpolated frames between the original ones to allow for smooth transition between frames. This can be accomplished with 'slowmoVideo'. The user interface allows you to select which parts you want faster or slower by changing the slope of the line describing the relative speed of the rendered video vs. the original one. As an example of how the GUI looks like:


And a demo of the resulting video, where we slow down the part from second 2 to 5 approximately (again left: original; right: modified video).

Thursday, 7 May 2015

GPX hyperlapse

In the last post I described how to create a virtual hiking tour (http://angel-de-vicente.blogspot.com.es/2015/04/creating-visual-tour-of-hiking-tour.html), and then I wondered if I could do something similar, but at street level, taking images from Google Street View. The goal was to take GPS data (either from a route that I have followed myself on foot, on bike, etc. or from data points generated by the driving directions of Google maps).

If we want driving directions from Google Maps, one easy way to generate GPS data points in a .GPX file is to:

  • use google maps to generate a route


  • grab the URL generated above in Google Maps and feed it to GPS Visualizer to get a .gpx file with the GPS data points following the given route


If, instead, we want GPS points from a route that we did previously, we can just, for example, download the GPX file directly from Endomondo.

At these point, we can try our luck with sites like http://gpxhyperlapse.com/ or http://alban.atchoum.fr/hyperlapse/ but if we want finer control, we will need to do some extra work.

If we use the .gpx files above, the results will not be very good, since there will be not many data points and/or due to the GPS receiver limitations, the points can be outside roads, resulting in shaky and jumpy street view images. So, we can do two things to improve this.

First, we are going to generate more data points by interpolating, with GPSbabel  (see http://www.gpsbabel.org/htmldoc-development/filter_interpolate.html) For example, to get data points every 5 meters:

gpsbael -i gpx -f track.gpx -x interpolate,distance=0.005k -o gpx -F newtrack.gpx

Second, we can try to fit those GPS points to a proper road (assuming we were on a bike tour, running, etc. on roads). To do this, we can use the site https://mapmatching.3scale.net/. You need to apply for an API key. Once you do that, we can convert our original shaky GPS data with

curl -X POST -H 'Content-Type: application/gpx+xml' --data-binary @newtrack.gpx "http://test.roadmatching.com/rest/mapmatch/?app_id=YOUR_APP_ID&app_key=YOUR_APP_KEY&output.waypoints=true" -o output.xml 

This works OK with the GPX file from Endomondo, which has time stamps, but it will break (the developer knows about this, so perhaps it is fixed when you try it) for the Google Maps generated GPX file, which doesn't have time stamps. To fix it, we just have to add timestamps to the GPX file before accessing test.roadmatching.com. So I turn all the trkpt from something like:

to something like (you can just put the same timestamp for all the trkpt's):

The output from TrackMatching matches the roads much better, hopefully, than the original GPX data, but it comes in an unfamiliar format that I have not been able to convert easily to a GPX file (if you know how to do it with some GPS conversion software, please let me know). I was too lazy to write a script to do the necessary transformations automatically, and instead I used search/replace in my text editor, so that from the output.xml file I extract all the wpt ....="" stuff and modify each waypoint of the form wpt y="28.465961" x="-16.269638" to (if you don't know how to do search/replace with regular expressions, this is a good time to learn!):

Now in the .gpx file obtained with GPSBabel, delete all the trkpt entries (so there will be an empty section, and in their place put all these new trkpt definitions.

With the help of GPS Visualizer again, you can verify that the new .gpx file is matched to roads better than the original one. Just use the options to convert a .gpx file to Google Maps as follows:



As an example, this is how the track looks like with the .gpx file directly downloaded from Endomondo:



And this after massaged by TrackMatching:



Now that we have .gpx files with many data points and nicely following roads, it is time to get the Google Street View images to put everything together. The code I used is a minimal variation of the code available at http://pastebin.com/FnsY8QFR (discovered at http://www.cyclechat.net/threads/my-cycling-video-c-o-python-strava-google.135566/). In case the pastebin expires, I uploaded the code to GitHub (https://gist.github.com/cac3a4434c4bd5b756ea.git). You can download it with:

git clone https://gist.github.com/cac3a4434c4bd5b756ea.git


The file gpxhyper.py is still very crude, so you will have to change things by hand. It will work by default on a file called input.gpx (either change that or make a symbolic link named input.gpx pointing to the file you want to work with). Next, leave uncommented the appropriate line: for the .gpx file coming from Endomondo:

gpx_trackseg = gpx_file.getroot()[1][3] # For Endomondo .gpx

For the .gpx file coming from Google Maps:

gpx_trackseg = gpx_file.getroot()[3][1] # For GPSBABEL .gpx

You should also have a Google Street View Image API Key (if you don't have one, you can get instructions at https://developers.google.com/maps/documentation/streetview/#api_key). Put it in gpxhyper.py (in place of YOUR_GOOGLE_API_KEY) and execute it as:

python gpxhyper.py

and all the corresponding images will be downloaded from Google Street View.

From these images use your favourite method to create a video. For example:

avconv -r 10 -i %5d.jpeg -b:v 1000k input.mp4

(Downloading the images takes a while, so make sure you calculate the appropriate rates beforehand. I found that around 10 FPS is a good number above (the images are taken at intervals, and if you try a normal video rate of 24FPS, the video will be too shaky). Assuming the 5 meters interval given to GPSBabel above, this will mean a virtual speed of 50 m/s or 180 km/h. Depending on the effect you want to create and the place where the images are taken (for example a city vs. a very open road with nothing near), this might be too fast or too slow. You will have to experiment a bit).

As an example, here it is the result for an Endomondo generated track (a bicycle route):




And here for a .gpx file generated via Google Maps as explained above (for this one, at the beginning there were some 'bad' frames that I removed by hand with Kdenlive video editor):




Thursday, 30 April 2015

Creating a virtual hiking tour

Lately I'm back to hiking and when I do a route I use my mobile phone with a GPS application (at the moment I use Wikiloc) to record the track. Last week I climbed Teide, and Wikiloc offers the possibility of easily embedding the data from their site, which is nice:



But you shouldn't stop there and you can also create a guided tour of the route. Doing it is quite simple, and I list below the steps I took to create the video tour of the Teide climb:

  • First, download the file from Wikiloc in KML format, and open it with Google Earth. Then, select the route and you can see an icon in the right part of the left panel that says "Play Tour" (play it a couple of times first to cache all the necessary images). Options to modify the speed, camera angles, etc. are available at Tools::Options::Touring.
  • Then, when we are ready, we just play it and at the same time record it with, for example, Kazam (30 FPS gives pretty good quality).
  • The resulting file is pretty big. To make it smaller we can use WinFF with output as MPEG-4 (very high quality). As an example, from 656MB this turns the file into 71MB, which can now be mildly edited with Kdenlive, and we render it all together for Web site::YouTube 1280x720. The resulting video is now 51MB, and we can upload it directly to YouTube: https://youtu.be/HgzXkAG7XjM 




Thursday, 4 November 2010

The best video editor in Linux? OpenShot vs. Kdenlive? The jury is still out.

I'm starting to create videos more or less frequently, and the software that I tried so far, while good, was not working that well. My video editing needs are simple: put together a number of sequences, mix them with audio, apply some effects and transitions (fade in, fade out, etc.), and add some titles, credits, etc., and export it all to different formats: for Internet, DVD, etc.; and obviously, all within Linux.

When I start creating videos more regularly, perhaps I change my mind about the software I've used, but so far the fight is between OpenShot and Kdenlive. The video editing software that I have used:

Pitivi, was behaving properly, it looked stable and easy to use, but transition effects are not yet implemented, only fades.


Open Movie Editor, the interface does not look very polished, although it was quite OK for some of the videos I did, but the main problem for me was that I didn't find a way to zoom in or zoom out in the timeline. This was a problem when I had very short clips (perhaps just a few seconds), because it would be difficult to select them, in order to move them around or cut them, etc.

Kdenlive, this is probably the most complete editor of them all, and it has all those features that I want and many more, but in the past it crashed on me many times. Today I'm trying it again, with a new version (0.7.7.1), which apparently squashed many bugs, and it looks very promising. If I manage to run this version without crashes, this is probably the one that will win my heart. A screenshot of the video I'm editing right now:



OpenShot is the new kid in the block, and the interface is quite clean and it is feature rich. So far, my only gripe is that if I apply effects to consecutive video clips, sometimes artifacts are created in the audio, but I think this also happens with Kdenlive, so I need to investigate. The screenshot of the same project with Openshot:

 
There are a few more programs to try, (see http://en.wikipedia.org/wiki/Comparison_of_video_editing_software), but I think one of these two is going to be more than enough for my needs.