How to detect whether a video is shaky I need to stabilize a video only when the video is shaky. 

I have successfully stabilized a video using FFmpeg, and I do not need to stabilize all the videos, I only need to stabilize the videos which appear shaky (not stable). 

Is there any way I can detect that?
Try ffmpeg's deshake filter. It scans the full frame (or a portion of your choosing) and detects shake based on a number of adjustable variables. If the video isn't shaking, it doesn't change the content. You can run the default settings with:

ffmpeg -i [input] -vf deshake [output]

Great little demo here. It's a combination detection and repair tool, no need to separately identify videos then reprocess.