There is no definite guarantee way to escape the hook. It is purely RNG in the beginning part of the game. When it says "Attempt to escape" it is a 4% chance that you can escape. If you try this and fail then the dying process speeds up and you go into the struggling space phase. When you enter the struggle phase your dying process will slow down and your fellow survivors can help you.
Additionally, some perks give you increased luck or other advantages to escaping the hook that stack on top of the base 4% chance to succeed escaping the hook.
This above part is from pure experience. However on the wikipedia it says that :
The first time a survivor is hooked they enter the normal phase. In this phase the player has a low chance of escaping. If the player escapes and is hooked a second time they will enter the struggle phase, in which the player has to resist the entity trying to stab him/ her by pressing the spacebar multiple times. If the player is hooked a third time they will die instantly.
After anymore googling I could not find any more information. I hope this helps you!