Raw video is an enormous flood of picture data, frame after frame. Raw audio is a long, dense stream of sound measurements. Without a way to shrink all of that data, a single short clip would fill your phone, and sending it would take far longer than watching it.
A codec solves that problem. It is a set of rules for squeezing video and audio into a much smaller file, and for opening that file again so it can be played. When you press play, send a voice note, or join a video call, a codec is working in the background.
What a codec actually is
The word codec comes from coder and decoder. A codec encodes, which means it packs raw video or audio into a compressed file. It also decodes, which means it unpacks that file when it is time to play it. The same rules do both jobs, so a file made with one codec needs that codec, or one that understands it, to open it.
Think of it like packing a suitcase. The raw recording is everything you own spread across a room. The codec is the method for folding it down and fitting it into the case. Decoding is opening the case at the other end and taking everything out in the right order.
How compression shrinks a file
Compression removes what is not needed and describes what is left in a shorter way. Video is a good place to see this, because so much of it repeats. In most shots, the background stays the same from one frame to the next, and only a small part of the picture moves. Instead of storing every frame in full, a codec can store one complete frame and then record only what changes after it.
Audio works in a similar way. A recording holds more detail than the ear can notice, so a codec can drop the parts that are hardest to hear. The result may be a tenth of the size of the raw recording, or smaller still.

Lossy and lossless: two kinds of squeeze
Codecs fall into two broad groups, and the difference is what happens to the detail that gets removed. A lossy codec throws some detail away for good. Once it is gone, no unpacking will bring it back. Most video and audio you meet every day uses a lossy codec, because the savings are large and the lost detail is usually hard to notice.
A lossless codec squeezes the file without throwing anything away. It finds shorter ways to describe the same data, like writing the same sentence with fewer words but keeping every meaning intact. The file ends up smaller than the raw recording, but not nearly as small as a lossy version.
For watching, listening, and sharing, lossy is usually the right choice, because the files are small and the quality is good enough for the moment. For editing and archiving, lossless is often better, because you want to keep every detail while you work. Edit a lossy file again and again, and each round of squeezing throws away a little more.
Containers and codecs are not the same thing
A codec and a container work together, but they are not the same job. The codec is the method for squeezing the video and audio. The container is the box that holds the squeezed video, the squeezed audio, and extra parts such as subtitles, all in one file.
A familiar container is MP4, which you will see on many video files. An MP4 file can hold video squeezed with one codec and audio squeezed with another, and the container keeps them in step so the sound matches the picture. Another file with the same ending can hold a different pair of codecs inside, so two files that look alike can behave differently.
Why codecs matter when you share or stream
Codecs are the reason streaming works at all. A video service does not send you the raw recording. It sends you a squeezed copy, and your device unpacks it as it plays, a few seconds at a time. A better squeeze means the same picture can travel over a slower connection without stopping.
The same logic applies when you share a clip yourself. A short video straight from a camera can be large enough to stall an upload or to eat through a data allowance in minutes. Squeezing it first makes it small enough to send quickly, and the person at the other end can play it without waiting for a huge download.
For creators, the choice of codec shapes the whole workflow. A video podcast recorded in a heavy, lightly squeezed format will look superb while you edit it, but the finished file will be far too large to upload as it stands. The final step is to squeeze it for sharing, which is a different job from recording it well.

What to do when a file will not play
If a video or audio file will not open, the codec is the first thing to suspect. Try a different player, because players carry different sets of codecs, and one may understand the file when another cannot. If the file plays in one app but not in another, the file itself is probably fine; the first player simply lacked the right decoder.
If no player will open it, converting the file is the next step. Conversion means unpacking the file with one codec and packing it again with another, usually into a container most devices understand. Expect a small loss of quality if you convert from one lossy format to another.
When you make something to share, keep your working copy at the highest quality you can while you edit, and only squeeze it down at the very end. The best apps for video editing will handle that final squeeze when you export, so you do not need to choose the settings by hand. Before you send a file on, play the squeezed copy yourself, so you catch any problem before your audience does.

The short version
A codec is a set of rules for squeezing video and audio into a smaller file and opening it again later. It comes from coder and decoder, and it does both jobs. Lossy codecs throw some detail away to make files much smaller, while lossless codecs keep everything and save less space. The codec is not the same as the container, which holds the squeezed video and audio together.
Next time a video plays smoothly on a weak connection, or a song fits easily on your phone, that is the codec doing its work. If you are making something yourself, remember the simple order: record and edit at the best quality you can, then squeeze at the end for sharing. If you want to put that order into practice, start with how to shoot a short film, where keeping quality high until the final export pays off in every shot.
