On Wednesday night, staff at Edinburgh's Cineworld multiplex will roll out the red carpet, break out the popcorn and prepare for the first night of the Edinburgh International Film Festival. The stars will arrive, the flashbulbs will wink. The opening movie is called Wah-Wah and Thursday's papers will be filled with pictures of its director, Richard E Grant, and the cast members who are attending the screening, among them Gabriel Byrne, Julie Walters, Emily Watson and Celia Imrie. Everyone will look happy to be there.

Grant is just one of many film-makers travelling to Edinburgh this month. Some of the other arrivals, like horror maestro Paul Schrader, bring with them a loyal following and a decades-long reputation.

Others bring only hopes - of securing that all-important distribution deal or, if the deal's already in place, making a critical splash and garnering some juicy quotes for the poster. "A triumph"; "The best thing you'll see this year"; "Calendar Girls-meets-Apocalypse Now". That kind of thing.

But hidden among the sales agents, filmmakers, producers, distributors and media executives is another group. Scan the list of industry delegates accredited to the Edinburgh International Film Festival and you'll tease them out. They are the practitioners of the black art of film festival programming: directors of the scores of film festivals which now straddle the world and occupy every week of the year.

The director of the Borderlines Film Festival - "the UK's biggest rural film festival" - is coming. So too are the directors of the Hof International and Emden film festivals, both in Germany. The Cardiff Screen Festival and the Thessalonika Film Festival are sending two delegates each and there are also representatives from film festivals in Belfast, Rotterdam, San Francisco and Osaka.

What they're doing here is exactly what Shane Danielsen, artistic director of the Edinburgh International Film Festival, is doing when he travels: sifting through film after film, at festival after festival, to find the ones that will make their event just that little bit special. Danielsen has been to 19 countries this year, and seen around 700 films.

His colleagues (and rivals) from the other festivals will have been doing much the same over the past year. For them, Edinburgh is just another stop on that never-ending tour. They'll tell you it isn't competitive, but it is.

So what happens when you see a film you want? Sometimes it's all very friendly.

Sometimes the deals are done on a handshake over dinner. Danielsen has already programmed the first movie - a Slovenian film - for next year's festival. He saw it, liked it, shook hands with the filmmaker and said: "See you in Edinburgh in August."

"That was the way it used to be done, " says Danielson. But it's different where a big-name film or director is concerned. He recalls the experience of a friend who ran the Sydney Film Festival in the 1960s. "He'd go to the Venice Film Festival and if he liked the new Antonioni film he'd go for dinner with Antonioni and that was that. Now you never get near those people because there are too many interests in play. You're dealing with their agent, or the sales company or, more likely, the distributor if the film has UK distribution."

When that happens, contracts need to be drawn up, marketing strategies consulted and commitments made to hold press conferences and parties, and find hotel suites for cast and directors. That's when it can turn dirty as festivals fight for big films, exposing rivalries that go back decades or which have been freshly minted in the white heat of a film world that seems to throw up more festivals yearly. You want to bring the wife and the chihuahas? No problem.

The rivalries between festivals are intense but there is a pecking order, according to Tim Dams, news editor of Screen International. He ranks Cannes as the world's pre-eminent film festival, with a position which is "pretty much unassailable", and below that he sees a second tier consisting of Venice, Berlin and Toronto. The Canadian city may look like an unlikely inclusion alongside the well-established European giants, but it has found favour with many American distributors and is seen by the Europeans as a good platform from which to launch into the North American market. On the third tier are festivals such as Locarno in Switzerland, San Sebastian in Spain, London, Moscow and Edinburgh.

The smaller festivals can't compete with Venice or Cannes or Berlin and don't try to.

But the question of exclusivity is still relevant. There is a feeling that a film is damaged goods if it has been unveiled somewhere else first. Will Rotterdam take Wah-Wah if Osaka shows it first? Perhaps, but not if Belfast has had it too. Do the distributors of Film X like the director of Festival Y? No? So will they offer the film to Festival Z purely out of spite? It happens.

Where the process can become especially dirty is in the competition between the various British film festivals for the (few) high-profile British premieres. You have to play hardball "occasionally", says Shane Danielsen. "But there are other film festivals who play hardball much more than we do."

London is one he mentions. In UK terms the Edinburgh film festival has seniority, but many American distributors prefer the glitz of a Leicester Square premiere so the London Film Festival, held in October, is often preferable. Moreover, London's director, Sandra Hebron, has been credited with reinvigorating that festival and last year scored a coup when she hosted the UK premiere of Wong Kar-Wai's 2046. The film had shown at Cannes and had been scheduled to close the Edinburgh festival, as the director's previous film had done, but was pulled at the last minute.

"I was annoyed that we had been deceived, " says Danielsen, still smarting from the loss. "I felt that promises had been reneged upon. It's dirty, but business is dirty."

London also showed the UK premieres of The Incredibles, the animated film from the makers of Toy Story, and Chinese martial arts epic House Of Flying Daggers. The opening film was Mike Leigh's Vera Drake, which had won the Golden Lion at the Venice Film Festival a month earlier.

So how low will a film festival director stoop to bag a film he wants? "I've been known to give up on films because at the end of the day I like to be able to look myself in the eye, " says Danielsen. "I don't see the point in behaving like a complete prick just for the sake of winning a victory."

An upstart on the UK scene is the Cambridge Film Festival, a thorn in Edinburgh's side as it falls in July, just weeks before the start of the Edinburgh festival. It means the two festivals often find themselves competing for the same films, as many festival line-ups are dictated by the release dates that distributors set for specific films. So a distributor releasing a starry British film in September, say, has a choice of launchpad: Cambridge or Edinburgh?

Heightening the various inter-UK rivalries is the fact that particular distributors sometimes favour one festival over the other - for reasons that can be personal as much as commercial.

Here the personality of the director is crucial. Rightly or wrongly, film festivals often reflect the character of the person who chooses the films. A smallish, third-tier festival like Edinburgh, which has little glitz and less cash, can still offer its history, its maverick reputation, the topography of the city, even the promise of a round of golf at St Andrews. But the director still has to be able to sell the festival, seduce the big names, beat Rotterdam/Belfast/Osaka to the prize find.

Perhaps the people with the best experience of film festival directors are the film-makers themselves. Richard Jobson's directorial debut, 16 Years Of Alcohol, played at the 2003 Edinburgh film festival and at other festivals across Europe and North America. His second film, The Purifiers, played at Edinburgh last year. His new film, A Woman In Winter, isn't showing in Edinburgh but he hopes to show it at the London film festival.

"Some directors rather pompously see themselves as curators and some see themselves as programmers and I think here is a difference between the two, " he says. "Curating is done from a very subjective point of view, everything's about their taste, whereas a programmer will think about an audience."

Predictably, Shane Danielsen doesn't agree. He's curatorial and proud of it.

"Absolutely my role is curatorial, " he says.

"Anyone can programme. Curating is a different beast and hopefully a more sophisticated one."

To illustrate his point, he recalls an illuminating lunch meeting with the director of the Cannes Film Festival. "He told me that everything had to be watched by a screening committee and there were more people on that committee than are on the security council of the United Nations.

No disrespect to Cannes, but I think one of the problems film festivals face is that they programme by consensus and committee instead of having a personal vision. Very few film festivals speak with one voice."

The danger with the curatorial approach comes when that voice fails to resonate with audiences and critics. After all, film festivals can wax and wane: Venice used to be unassailable as the world's second most powerful festival but it has lost ground to Berlin and Toronto. "Toronto's reputation has grown massively, " says Screen International's Tim Dams. "Robert Redford's Sundance Festival is another one. It came from nowhere 20 years ago and is now a really important American festival."

So the old guard can't rest on their laurels.

The Edinburgh International Film Festival turns 60 next year but it is being nibbled at - by Cambridge, by Sarajevo, by others. It could struggle to maintain its position as the axis of influence shifts away from Europe towards the Far East, or as the pressure builds to up the glamour quotient in our media-saturated world. "Reputations do come and go, " says Dams. "If the Cannes selectors had a disastrous few years then people would start to question it."

And if it could happen to Cannes, it could happen to anyone.

The Edinburgh International Film Festival opens on Wednesday www. edfilmfest. org. uk Critics' choice: page 29