Comcast announced on Thursday an agreement to acquire Time Warner Cable for more than $45 billion in stock, a deal that would combine the biggest and second-biggest cable television operators in the country.

For Comcast, which completed its acquisition of NBC Universal, the television and movie powerhouse, from General Electric less than a year ago, the latest deal would be its second big act to radically reshape the media landscape in the United States. And the merger is almost certain to bring to an end a protracted takeover battle that Charter Communications has been waging for Time Warner Cable.

Brian L. Roberts, the chief executive of Comcast, said in a statement on Thursday that Time Warner Cable’s executives “have created a pure-play cable company that, combined with Comcast, has the foundation for future growth.”

For Time Warner Cable, the deal provides a neat solution to its problems. It will receive just about the $160-a-share price it said was its true value, and possibly more. It will no longer have to slog ahead with a turnaround plan being run by a new chief executive, Rob Marcus. And it will allow it to become part of the company that is already the dominant force in cable television services.

Despite combining the two largest cable operators in the country, a merger may have little impact on consumers. Comcast and Time Warner Cable do not compete directly in any markets.

Nonetheless, regulators will surely look carefully at the impact on the deal to consumers, and may also focus on whether the combined company will have additional power in negotiations with cable networks, a recent source of tension in the industry. The two companies said they expected the deal to close by the end of the year.

Comcast has about 22 million television customers, according to the National Cable and Telecommunications Association. Time Warner Cable has about 11 million video subscribers, according to people familiar with the company.

In a bid to appease antitrust regulators, Comcast is expected say it is willing to divest three million of Time Warner Cable’s roughly 11 million pay television subscribers.

It was not immediately clear if Comcast would propose certain markets to divest, but shedding those subscribers should keep Comcast with less than 30 percent national market share for pay television, a level the company believes will satisfy antitrust regulators.

Under the terms of the deal, Time Warner Cable shareholders will receive 2.875 shares of newly issued Comcast common stock for each of their shares, people briefed on the matter said on Wednesday evening. Based on Comcast’s closing price of $55.24 on Wednesday, that values each Time Warner Cable share at about $158.82 each.

If Comcast stock rises on news of the deal, the price could go up, while if the stock falls, it could go down.

The deal is subject to approval by both shareholders of both companies. Because it is an all-stock deal with newly issued shares, Comcast will not have to take on any new debt.

Should a deal be completed, Time Warner Cable shareholders will own roughly 23 percent of the combined company.

For Comcast, the deal, extending its leadership in the cable industry, is another transformative step in the evolution of what is now America’s most influential media company. It comes less than a year after Comcast completed the acquisition of NBC Universal And it will further extend Comcast’s lead as the nation’s largest cable operator.

Charter, however, is left in the lurch. Charter is one of the nation’s smaller cable operators, but had big ambitions to compete with Comcast. Last year Liberty Media, the conglomerate backed by billionaire John C. Malone, acquired 27 percent of Charter, and urged the company to pursue deals, a process that could have led Mr. Malone back to the heights of American cable, an industry he once ruled as head of TCI, then the country’s largest operator.

Beginning last year, Charter made overtures to Time Warner Cable, privately offering a succession of higher prices, all of which were rejected. Last month, Charter went public with an offer of $132.50 per share for Time Warner Cable, which Mr. Marcus dismissed as inadequate. He countered by saying the company would consider a bid of $160 per share, which Comcast has now nearly matched.

On Tuesday, Charter nominated a full slate of directors to Time Warner Cable’s board, its most aggressive move to date.

At the same time, Charter was negotiating with Comcast about a deal through which Comcast would acquire certain markets from Time Warner Cable if Charter was about to buy the entire company. Comcast, however, had other plans.

Charter may buy the subscribers that Comcast has pledged to divest, giving it added scale but not nearly the boost it had hoped when it set its eyes on acquiring all of Time Warner Cable.

Though the companies are set to announce the deal, it could still come undone. Shareholders of either company could vote it down, though that seems unlikely.

Charter could still play spoiler with a new bid, though that seems unlikely too, given its limited financial resources.

Or Comcast shares could collapse, leading Time Warner Cable shareholders to shun a deal.

Advising Comcast are JPMorgan Chase, Barclays and Paul J. Taubman, the former top Morgan Stanley deal maker. Davis Polk & Wardwell and Willkie Farr & Gallagher are its legal advisors.

Morgan Stanley, Allen & Company, Citigroup and Centerview Partners are financial advisers to Time Warner Cable and its board. Paul, Weiss, Rifkind, Wharton & Garrison and Skadden, Arps, Slate, Meagher & Flom are legal advisers.

The two companies will hold a conference call at 8:30 a.m. on Thursday morning.