Akshat 👉

4 Subscribers

Video Recommendation System - CodeWithAkshat

Akshat | Oct. 24, 2025
Views 94 | Likes 9

In this post, we are going to see how the set of videos is recommended to the user, the mathematical concept supporting the system for the smooth functionality, and we will also discuss about step-wise algorithm implementation to generate the result with a small code segment at the end.

We will also discuss about future prospects, pros, cons and feed generation.

One of the most important parts of a platform containing video sharing and streaming is the recommendation system. Imagine if you are playing an educational video and all of the recommended videos are of gaming. It is not just unconventional, but also seems bizarre, because you are there to watch educational content, not gaming videos. To solve such issues, we need a strong recommendation system, which can sequentially generate the most desired set of videos.

 

Mathematics involved:

    In any prediction system, mathematics plays a crucial role in choosing targets, finding relevance scores, sorting and applying different distributions, etc. The CodeWithAkshat website employs a clever technique that utilises the Greatest Common Divisor (GCD) of a number and its prime factors to find the most relevant videos.

(CGD) Greatest Common Divisor: Is defined as the largest positive integer that divides two or more numbers without leaving any remainder.

Prime factorization: Is the process of breaking down a whole number into the product of its prime factors, which are prime numbers that cannot be divided by any other number besides one and itself.

Concept:

  • Finding the GCD of two numbers will generate the credibility score.

  • This credibility score is combined with the relevance score, usually taken from another matrix sorting the preferences, a particular user like to watch.

  • The final score is sorted through the database only while the video page is being served, increasing the overall loading speed.

The key takeaway is in assigning these numbers A and B for an videos. These numbers A and B are ContentIDs for the video, particularly generated as multiples of prime numbers.

Ex:

 

GCD(A, B) = 6 (Which means there are two videos related to each other with the factor of 6)

 

GCD(A, C) = 30 (Which means there are two videos related to each other with the factor of 30; the video C contains all the content types as the video A)

Generating content IDs using prime numbers helps in the uniform generation of all the IDs from categories/tags assigned to a video.

Implementation segment:

  1. Since SQL does not have an inbuilt function for finding the GCD of a number, we need to define our own GCD function in SQL Functions.
  2. At the time of uploading a new video, a function is called that uses the preloaded {content: prime} number relation to generate the content ID and assign it to the video.
  3. There are a lot of operations involved, but we need to focus on the SQL query written using Django ORM + loose MySQL query that handles the desired operation.

 

def video_playing(request, slug):
    video_found = Video.objects.filter(slug=slug).first()
    comments = video_found.videoComment.all().order_by("-date")

    if video_found:
        video_found.tviews += 1
        video_found.save()

    more_videos = Video.objects.filter(visi=True)
    pl_videos = 'None'
    if video_found.playlist:
        pl_videos = Video.objects.filter(playlist=video_found.playlist)
        more_videos = more_videos.exclude(playlist=video_found.playlist)

    # fetching related videos
    factor = video_found.categoryId
    more_videos = more_videos.annotate(gcd_value=RawSQL("GCD(categoryid, %s)", (factor,))
                                       ).order_by('-gcd_value', '-tviews')[:10]

    # more_videos = list(more_videos)
    # more_videos = random.sample(more_videos, len(more_videos))

    context = {'name': video_found, 'types':video_found.source[8:23], 'comments':comments, 'plvideos':pl_videos, 'mvideos':more_videos}
    return render(request, "opcoder/video_playing.html", context)

 

Pros:

This is the minimalistic approach to find the most favourable set of videos, which uses less computational power as compared with other machine learning algorithms.

cons:

Although the algorithm works completely fine but it may cause problems with videos more than 200 in DB or using more than 10 categories in a single video.

Future prospects:

To solve this overflow problem, we can use different normalisations and mathematical distributions for better handling of data.

 

Thank you very much for taking the valuable time yours read this article. Please comment and like this article so that I can better understand your views.

Comment Section

very good from TS

cybernovaguy | 31-03-2026

Bhai kab sa !

akshat9 | 23-02-2026

Bhai i am looking for this blog only

rudrag_05 | 21-02-2026

comment

Akshat | 18-11-2025