'An Inconvenient Truth' has been viewed 1.5bn times on YoutubeThe order will come as a surprise to millions of people who have used the video sharing site to view footage of TV shows, films, sports events, music clips and home videos.
Figures show that in April alone more than 130 million people across the world visited YouTube including 11.6 million in the UK. The judgment is part of an ongoing legal battle between the internet giant, which owns YouTube, and Viacom, which owns Paramount Pictures and MTV Networks.
Viacom accuses Google of allowing millions of users to illegally post and watch clips of its TV shows and films, such as South Park, on the popular video-sharing site, and is trying to build evidence that suggests that the sharing of illegal material on YouTube is the cornerstone of its business.
Google must now hand over a "user log" to Viacom. The log will contain users' YouTube log-in details, the IP address of their computer - a unique code that identifies individual machines - plus details of all the video clips that users have viewed.
The judgment, which was made on Wednesday, could apply worldwide, and affect more than four million registered YouTube users, as well as potentially those who have simply watched clips. Internet freedom campaigners have criticised the decision, with the Electronic Frontier Foundation calling it a "setback" for online privacy rights.
Viacom believes that YouTube has illegally hosted copyrighted material from a variety of shows, and claims that the internet has led to "an explosion of copyright infringement" on YouTube and other video-sharing sites. It said that the Al Gore documentary, An Incovenient Truth, had been viewed 1.5 billion times on YouTube.
However, Google argues that it already far exceeds "its legal obligations in assisting content owners to protect their works", and that Viacom's $1 billion (£500 million) lawsuit is a threat to internet freedom.
Google, which bought YouTube for $1.65 billion (£820 million) in 2006, said that it abided by the rules of the Digital Millennium Copyright Act, which states that as long as sites such as YouTube remove copyright content as soon as it is brought to their attention, they are protected by law from prosecution.
But Viacom argues that YouTube has done "little or nothing" to stem the flow of copyrighted material, and the company said it had identified more than 150,000 unauthorised clips of its content on the video-sharing site.
However, the US court that made the ruling said that privacy concerns raised by the judgment were purely "speculative". A spokesman for Viacom played down fears that personal information would be shared and that user logs would be used to prosecute YouTube users.
"Only our legal team will have access to this data," he said.
"We will not be using to go after individuals."
Google welcomed the restrictions the court placed on the use of the logs. "We are pleased the court put some limits on discovery, including refusing to allow Viacom to access users' private videos and our search technology," said Catherine Lacavera, a member of Google's legal team.
"We are disappointed the court granted Viacom's overreaching demand for viewing history. We will ask Viacom to respect users' privacy and allow us to anonymize the logs before producing them under the court's order."
© Copyright of Telegraph Media Group Limited 2008