There are no federal laws about it.
Technically, it varies from state to state. But in practice, it’s legal to film police while they are on duty.
The laws on filming people with or without their consent vary from state to state. In some states, you only need the consent of one party, so the person with the camera can give that consent.
In other states, all parties must consent. Such states usually have exceptions for expectations of privacy. In public areas, you have no expectation of privacy, so consent is not really needed, especially for uniformed police officers, who are almost always working in public. But Massachusetts and Illinois have no such exceptions, meaning you need the consent of the filmed. This led to people being arrested in those states.
That led to some court cases up to their respective Federal Appeals Courts that ruled that there is a legitimate 1st Amendment argument for allowing the filming of on-duty police.
The State of Illinois tried to appeal up to the Supreme Court, which upheld the Appeals Court ruling by deciding not to hear the appeal.
That said, people are still being arrested for it. Why? Who can say for certain, but I usually attribute most cases to ignorance rather than malice.
Gut feeling, but there probably are cops out there who actually do maliciously persecute people who film them, but I doubt the majority of them do. The minority who are malicious will, of course, receive the majority of the attention. The majority are regular Joes just doing their jobs like the rest of us. Unfortunately, regular Joes tend not to be the brightest bulbs, either, so they may arrest out of ignorance, rather than malice. Lawyers can’t keep every law straight, and there’s no reason to believe a regular police officer is trained in every single law, either.