U.S. District Judge Louis L. Stanton authorized full access to the YouTube logs after Viacom Inc. and other copyright holders argued that they needed the data to show whether their copyright-protected videos are more heavily watched than amateur clips.
The data would not be publicly released but disclosed only to the plaintiffs, and it would include less specific identifiers than a user's real name or e-mail address.
Lawyers for Mountain View, Ca.-based Google Inc., which owns YouTube, said producing 12 terabytes of data - equivalent to the text of roughly 12 million books - would be expensive, time-consuming and a threat to users' privacy.
The database includes information on when each video gets played, which can be used to determine how often a clip is viewed. Attached to each entry is each viewer's unique login ID and the Internet Protocol, or IP, address for that viewer's computer.
Stanton ruled this week that the plaintiffs had a legitimate need for the information and that the privacy concerns are speculative.
Stanton rejected a request from the plaintiffs for Google to disclose the source code - the technical secret sauce - powering its market-leading search engine, saying there's no evidence Google manipulated its search algorithms to treat copyright-infringing videos differently.
The court has yet to rule on Google's requests to question comedians Jon Stewart and Stephen Colbert of Viacom's Comedy Central.
Viacom is seeking at least $1 billion in damages from Google, saying YouTube has built a business by using the Internet to "willfully infringe" copyrights on Viacom shows, which include Comedy Central's "The Daily Show with Jon Stewart" and Nickelodeon's "SpongeBob SquarePants" cartoon.
The lawsuit was combined with a similar case filed by a British soccer league and other parties.
Together, the plaintiffs are trying to prove that YouTube has known of copyright infringement and can do more to stop it, a finding that could dissolve the immunity protections that service providers have when they merely host content submitted by their users.
Though Google said giving the plaintiffs access to YouTube viewer data would threaten users' privacy, Stanton referred to Google's own blog entry in which the company argued that the IP address alone cannot identify a specific individual.
In a statement, Google said it was "disappointed the court granted Viacom's overreaching demand for viewing history. We are asking Viacom to respect users' privacy and allow us to anonymize the logs before producing them under the court's order."
Google did not say whether it would appeal the ruling or seek to narrow it.
Stanton's ruling made only passing reference to a 1988 federal law barring the disclosure of specific video materials that subscribers request or obtain.
Kurt Opsahl, a senior staff attorney with the Electronic Frontier Foundation, said Stanton should have considered that law along with constitutional free-speech rights, including a right to read or view materials anonymously.
He said a user's ID can sometimes include identifying information such as a first initial and last name.
Viacom said it isn't seeking any user's identity. The company said any data provided "will be used exclusively for the purpose of proving our case against YouTube and Google (and) will be handled subject to a court protective order and in a highly confidential manner."
This is not the first time Google has fought the disclosure of user information it had been stockpiling. While gathering evidence for a case involving online pornography, the U.S. Justice Department subpoenaed Google and other search engines for lists of search requests made by their users.
After Google resisted, a federal judge ruled that Google was obliged to turn over only a sample of Web addresses in its search index, not the actual search terms requested.