Dumping options off the top of my head. Not necessarily mutually exclusive. All easier said than done.
>Go cold turkey.
Keep yourself from using YT or vidya for a set amount of time. Set a realistic goal for that time (a day? a week?).
Keep a timer right in front of you while you use YT or vidya. Make up and convince yourself that you can only spend a set time until you need to quit.
>Do the work first.
Try rewarding yourself with a single, small piece of candy for every homework problem or milestone achieved (20 minutes of focused work). Do not use a 5 minute YT video, it will lead to more videos.
Then watch YT once all of your work is done.
Work on being aware of what you're doing at all times. If awareness or focus lapses, analyze why.
Analyze why you spend so much time on YT. (Habit? Addiction? Lack of self control? Time killer? Procrastination anxiety [that's what I get]?)
If you share more thoughts on why you might spend so much time on YT and vidya, and how it interferes with school work, I can try comparing it to my own experience.