If you can’t beat them, join them.

Cable TV mogul John Malone, who for years has railed against the skyrocketing costs of sports programming, is working through his Liberty Media company to buy a large minority stake in the firm that owns Formula One racing, The Post has learned.

Liberty Global, the international arm of Malone’s empire, and Discovery Communications are working alongside Liberty Media to purchase as much as 49 percent of Formula One Group from private equity powerhouse CVC Capital, sources said.

While the talks are in the early stages and could eventually fall apart, the purchase of the global open-wheel racing circuit would put Malone on the other side of the table when it comes to sports rights fees.

Formula One Group, which rings up annual revenues north of $1.5 billion from its 19 races on five continents, including its glittering A-list favorite, Monte Carlo Grand Prix, is valued at between $7 billion to $8 billion, sources said.

Bernie Ecclestone, 83, Formula One’s longtime chief executive, stepped down from the board of the racing group’s parent in January after he was indicted in Germany on bribery charges. His trial is set to begin in April.

The purchase of a large minority stake by Malone’s companies would deepen the mogul’s footprint in sports.

Liberty Media owns the Atlanta Braves and in January, Discovery, in which Malone controls 29 percent of the voting power, announced it was increasing its stake in EuroSport to 51 percent from 20 percent.

EuroSport is a pan-Europe television network that is seen in 54 countries and, according to comments by Discovery CEO David Zaslav at the time of that deal, is looking to grow by adding new sports rights.

“Formula One could be used to drive EuroSport,” said one person close to the situation.

In June last year, Liberty Global acquired pay-TV and internet provider Virgin Media for $16 billion and last week purchased the balance of Dutch cable operator Ziggo for $9.44 billion.

The acquisitions are part of a global plan by Liberty Media to consolidate the cable industry and cut costs in the process. In the US, Liberty-backed Charter Communications is hoping to persuade Time Warner Cable shareholders to agree to a buyout offer that would create a larger and more powerful television distributor that could push back harder against rising fees for sports rights.

Back in November 2012, Malone told the LA Times, “We’ve got runaway sports rights, runaway sports salaries and what is essentially a high tax on a lot of households that don’t have a lot of interest in sports.”

Malone said the consumer and the cable operator are “really getting squeezed.”

Meanwhile, CVC Capital has a keen interest in sports. Last year, it lost out on a chance to acquire sports and fashion agency IMG Worldwide when its bid was topped by WME. It has owned the racing group since March 2006.

Reps for the media bosses and for CVC Capital didn’t return calls for comment.