Known as "sun kinks", this happens due to thermal expansion of the rails. When welded rail is installed, there is a procedure called "De-stressing" that adjusts the rails for thermal temperatures. It's a balancing act, you want them mostly in tension, but if you put too much tension on them, you'll have a "pull-apart" (which is exactly what it sounds like) during cold weather.
So you review the average temps in the area and using various guidelines, adjust based on the expected highs and lows. However, even if done correctly, things change, the rails slowly move and over time things can get out of adjustment.
What makes it especially scary is that the rail will be sitting there under great tension, ready to snap, and then the motion of the train vibrates things and causes it to break loose. So it can jump up at the last second, as you see in that first one. At that point, all you can do is hang on and hope for the best.