After years of on-again, off-again merger talks, broadcast giant CBS Corp. and its corporate sibling Viacom Inc. on Tuesday finally agreed to reunite in a nearly $12 billion deal that will bring together such well-known brands as CBS, MTV, Nickelodeon and Showtime.

CBS, which is the larger of the two companies and worth $18.5 billion, will absorb the smaller Viacom, which owns such assets as BET, Comedy Central and the Paramount Pictures movie studio in Hollywood. The new company will be called ViacomCBS Inc., in a nod to the legacy of Sumner Redstone, the ailing 96-year-old media titan who built an empire from a small chain of drive-in movie theaters in the Northeast.

Viacom Chief Executive Bob Bakish will become president and chief executive of the new entity, and gain a seat on the board. Shari Redstone, the mogul’s daughter, will become the first chairwoman in Viacom’s history.

The proposed merger of the two New York-based companies is the latest in the wave of entertainment industry consolidations and was widely expected. It was the third time in three years that CBS and Viacom attempted to hook up.

Last year, telecommunications colossus AT&T bought HBO, CNN, TBS and the Warner Bros. studio in an $85 billion deal. In March, Walt Disney Co. completed a $71.3 billion acquisition of much of Rupert Murdoch’s Hollywood holdings.

CBS and Viacom suddenly found themselves medium-sized players, no longer leaders of the industry. But it wasn’t just a consolidating industry and splintering audiences that drove the two companies together. Both were weakened by years of internal turmoil: boardroom battles, costly lawsuits, financial miscalculations and management woes.

ViacomCBS will be worth about $32 billion.

Investors responded favorably to the news. Viacom shares closed at $29.21, up 2.4 percent while CBS shares rose 1.3 percent to $48.70.

The Redstone family, through its Massachusetts-based investment vehicle, National Amusements Inc., controls nearly 80% of the voting shares of the two companies. The family firm already has approved the all-stock transaction. Existing CBS shareholders will own 61 percent of the combined company, while existing Viacom shareholders will own about 39%. Viacom shareholders will receive 0.59625 shares of CBS stock for every Viacom share that they own. On Tuesday, Wall Street valued Viacom at about $12 billion.

The new company will be one of the largest players in TV advertising, capturing an estimated 22% of viewership to traditional television. That will be in the same league as Comcast Corp., which owns NBCUniversal, and ahead Walt Disney Co. Combined, the company spends more than $13 billion a year to produce content. It will have a growing presence in the streaming space and access to a library stocked with more than 140,000 television episodes and more than 3,600 movie titles.

The company also will have international exposure with networks in Britain, Australia and Argentina. CBS’ premium channel, Showtime, will have access to movies from Paramount Pictures’ deep library, which includes such properties as “The Godfather,” “Top Gun” and “Transformers.”

“Even though Viacom and CBS will be a larger company, it will still have to compete with Disney, which is in a league of its own,” said Jordan Matthews, an entertainment lawyer with Weinberg Gonser in Los Angeles.

The merger, which was ratified overwhelmingly by the boards of both companies, requires the approval of government regulators, a process that is expected to take several months. The companies said they expect the deal to be complete by year’s end.

The corporate combination, expected to achieve $500 million in savings, will be a homecoming of sorts. The two companies have had a long and often turbulent history. CBS created what would become Viacom in 1952 as a vehicle to sell into syndication such popular shows as “I Love Lucy.” Regulatory rules forced CBS to divest the unit in 1971.

More than a quarter-century later, then-Viacom Chairman Sumner Redstone victoriously announced what he called “a merger of equals” between his company and CBS. In a September 1999 news conference, Redstone told journalists: “Viacom and CBS are natural partners ... we are siblings.” At that time, Wall Street valued a combined Viacom-CBS at $80 billion.

The marriage lasted just six years. In 2006, Redstone divided his empire, saying the two halves could stand on their own.

The billionaire from Boston was convinced the future was brighter for his Viacom, which owned cable TV channels and Paramount. Viacom was a darling on Wall Street and Madison Avenue because its networks — Nickelodeon, Comedy Central and MTV — drew younger viewers who were prized by advertisers. The breakup benefited Redstone, too, because he became controlling shareholder and executive chairman of two companies.

Time proved Redstone wrong.

“We have long believed that the initial separation of these companies made zero sense,” media analyst Michael Nathanson wrote in a recent report.

In the last several years, audience behavior changed drastically. Younger viewers were the first to flee traditional TV. Now, teenagers and young adults spend more time playing video games, watching video clips on their phones or shows from streaming services like Netflix and Hulu than MTV. Amid the migration, Viacom made a series of management blunders, including awarding its top executives hundreds of millions of dollars in compensation while under-investing in programming. Viacom’s brass sold its valuable Nickelodeon shows to Netflix, helping the Los Gatos, Calif., company build its audience.

Paramount also struggled under the leadership of the late Brad Grey, weathering a period of flops and enormous losses.

“The studio suffered over $1 billion in losses,” said Jeffrey Sonnenfeld, a professor at the Yale School of Management.

Viacom lost more than half its value. Five years ago, its stock traded for more than $75 a share. Viacom closed Friday at $30.01 a share.

Shari Redstone led a management shakeup at Viacom in 2016 that ousted her father’s longtime allies. She installed Bakish as chief executive of Viacom.

Since then, the 55-year-old executive has worked to improve Viacom’s corporate culture, invest in programming and make small acquisitions, including streaming service Pluto TV, to broaden Viacom’s reach. Bakish overhauled the management of the moribund Paramount film studio and tossed out senior management at the TV channels. In its most recent fiscal quarter, Viacom hit a milestone when it reported that domestic advertising revenue had increased for the first time in five years. The company also boasts this summer’s top cable series, “Yellowstone.”

“Shari and Bob saved Viacom,” said Sonnenfeld. “Bob is not a back-slapping cheerleader. He might be an anomaly in show business, which is filled with self-promoters, but he is a classic example of still waters run deep.”

Shari Redstone, pressed for a Viacom-CBS merger in 2016, and again last year.

She was rebuffed the first time because CBS’ management, including its former powerful chief executive, Leslie Moonves, and independent board members worried that the battered Viacom would be a drag on CBS. Then, last year, merger talks collapsed when CBS’ independent board members sued the Redstones in an unsuccessful attempt to strip the family of its controlling votes.

Amid the high-stakes legal fight, CBS became engulfed in a scandal. A dozen women accused Moonves of sexual harassment or assault. He denied the allegations but was forced to resign in September.

Since then, CBS has lost more than 10% of its value. The controversy led to changes in the CBS board, the appointment of Joseph Ianniello as the acting CEO, and, ultimately, the merger talks that began earlier this year and culminated this week.

Ianniello, 51, will remain in charge of the CBS properties, which include the broadcast network, production studios, a chain of television stations, the streaming service CBS All Access, CBS Films, Showtime Networks and the Simon & Schuster book publishing house. His new title will be chairman and chief executive of CBS.

Over the last decade, CBS has worked to adapt to the changes, while maintaining its status as the most-watched TV network in America with such popular shows as “NCIS,” “The Big Bang Theory” and “60 Minutes.” On the business side, the company shed mature assets, including its billboard division and radio stations. It was among the first to stream live programming. It invested to build CBS Television Studios and Showtime. CBS Television Studios now is one of the largest in Hollywood, producing 89 shows, up from 70 a year ago.

The new board will consist of 13 members, including six independent members from CBS, four independent members from Viacom, Bakish and two designees of National Amusements, including Shari Redstone.

But the challenges are steep, and Viacom has more than $8.5 billion in debt that must be absorbed.

Viacom-CBS still will continue to be a relative small fry — unless it combines with other independent studios, such as Sony Pictures Entertainment or a major telecommunications company like Verizon or T-Mobile.

Los Angeles Times