Online shopping and downloading have irreversibly changed the way we buy music.

As independent record shops battle to survive, could a new generation of vinyl enthusiasts be their salvation?

A cold Friday in January. As the last rays of the sun throw an unseasonable orange glow over Edinburgh's Grassmarket, James Byron and Conor Mackie step inside the Avalanche record shop, mooch past the CD stands near the door and, with the confidence of hardened regulars, set to leafing through the racks of vinyl.

The place isn't busy. Music plays over the speakers – the two friends will soon hear Mid Air, the solo album by Blue Nile frontman Paul Buchanan – and among the other browsers there is a studious, reverential hush. Everyone keeps their fingers moving, in part to stay warm but mostly because they know there's always another record or CD behind the one they're looking at, and it could be a lost gem. So on and on they flick. Maybe they'll buy some music, maybe they won't. Maybe they'll stump up for an Arctic Monkeys poster instead.

Mackie owns a turntable – "it was my granddad's" – and he's looking for anything by The Who, The Doors or Jimi Hendrix. Although Byron doesn't have a turntable he's saving for one. He's shopping for Primal Scream records and looking for anything by the Rolling Stones or the Fab Four. "I love The Beatles," he laughs. "I had a video of Help! and I used to watch it when I was two or three."

That, by the way, was not very long ago. Obsessed with the 1960s they may be, but Byron and Mackie weren't born when The Beatles disbanded or when John Lennon was shot. In fact, if the band was magically to reform and tour this pair wouldn't even get served at the bar. James Byron and Conor Mackie are both 15 years old: if it's true that the record shop is dying, they'll be wearing school uniform to the funeral.

Make no mistake – the independent record shop is dying. In his 2012 documentary Last Shop Standing, author and filmmaker Graham Jones estimates that around 500 have closed in the past few years leaving fewer than 300, down from 2000 stores in the mid-1990s.

On November 9, 2012, it looked like Avalanche would soon be one of those closing its doors when owner Kevin Buckle posted a headline-grabbing blog entry in which he said he planned to close on January 6, 2013. At length, he laid out the whys and wherefores of a decision that would rob the capital of one its most celebrated stores. "There are nowhere near enough customers to sustain a shop of our size in the city centre," he wrote. "I will seriously have to consider if Avalanche can continue as primarily a record shop as so much of our business has been eroded."

As it was, Buckle opened for two days a week during January and is now open full time again. In Aberdeen, however, it's a different story. Two days ago, on January 31, Raymond Bird closed the door on One Up after 34 years. "We can't compete online or with the supermarkets," he says simply. Stirling's Europa Music is Scotland's second oldest record store. It nearly closed six months ago and only stayed afloat through a drastic restructuring. Owner Ewen Duncan sees some cause for optimism – "We have a good range of ages coming in, from teenagers right through," he says. "A lot of kids are getting into vinyl, especially back catalogue stuff like Led Zeppelin and AC/DC." But it's a hand-to-mouth existence. Poor credit terms, unfavourable supply lines and (The Beatles would sympathise) an unyielding taxman see to that.

Back in Avalanche, Buckle is moving from counter to shelves and back again, talking constantly to his customers as he serves the few who are actually buying something, or chatting to regulars as they browse or hang about in that insouciant way people do in record shops.

"There's always going to be a place for a shop like this," he says defiantly. "I'm working towards something that will exist in 20 years because in 20 years people will still be listening to music. The kids will still be wanting posters and all the things you can't download."

Of course it isn't just the independents which are struggling. Specialist music retailers account for only one-fifth of sales, less than online retailers and digital platforms and only marginally ahead of supermarkets. But just days earlier the biggest player in that specialist sector, HMV, announced it was going into administration with the potential loss of 4000 jobs and the closure of 223 stores. Since then the company's debt has been bought by Hilco, a "restructuring specialist" which has done rather well out of the recession. Around half the UK's HMV stores are now expected to close. It's thought those in Stirling and Falkirk could be among them.

So is the demise of HMV the opportunity shops like Avalanche have been waiting for? Yes and no, says Buckle. "We have a loyal customer base but obviously you also have casual customers who would come here if they weren't at HMV. That would make all the difference. That would make our business far more solid just getting a small amount of their business."

But, he adds, the loss of HMV would be a short-term gain. "In the long term it wouldn't be good because the infrastructure breaks down. My point about the independent shops has always been that if there's six of us left doing really well, there's still no point because six isn't a business for anybody."

In other words a slimmed-down, nimbler HMV which puts its focus back on selling music could do the independent sector a favour. In fact some independents are already noticing higher sales, in part because of a backlash against Amazon's tax arrangements. For instance London's famous Rough Trade, comprising two shops, including a flagship store off Brick Lane, is reporting the best trading results in its 36-year history.

Avalanche trades on its brand, and its T-shirts are big sellers, but Rough Trade has taken things a stage further and its T-shirts are a collaboration with French fashion designer Agnes B. There's also a sweatshirt in the same line, as well as caps, totes and even a striped Breton top. You can buy Rough Trade-branded record bags by Manhattan Portage, another upmarket manufacturer, and there's an album club you can join on either a three, six or 12-month basis. Every month they send you the latest "must-have" album, plus bonus material.

Diversify or die seems to be the key phrase. Buckle isn't unaware of the maxim: the rail of vintage clothes is a relatively new addition and he is about to relaunch an album club. The sale of posters continues to be lucrative – some sell for as much as £15 and can account for up to 20% of turnover – and live events have proved popular. Around 500 people crammed in for an appearance in 2011 by Frightened Rabbit and that focus on local talent has paid off. When both HMV and Fopp had sold out of the new album by fast-rising Glasgow band Admiral Fallow recently, Buckle was able to call in a favour and have a box delivered – by the father of the lead singer.

There are dissenting voices, though. Most of those who posted comments below Buckle's threat to quit were supportive. But not all. "Avalanche ceased to be relevant about five or six years ago," wrote one complainant, who compared the shop unfavourably with Glasgow's Monorail. "I've shopped in Avalanche for 20 years and it used to be a pleasure," wrote another. "Now, it's a depressing experience."

You can't please all the people all the time, then. But perhaps a new generation is coming through with less stringent requirements, one which sees the record store as a boutique-style shopping experience where browsing is key. A generation which contains as many women as men.

Meet 19-year-old Flo Berten. She's from Durban in South Africa, recently started a law degree at Napier University and has become something of a regular. "I came in here and fell in love with it," she says. "I love the posters and the T-shirts. I just love the feeling of it, the vibe, all the old records. I like going to HMV but it's very commercial whereas this is about a love of music."

Is there anything she would change? "Maybe it could be a bit warmer," she laughs. "But no, not really. It's fun."

Another browser is Ellinor Fristorp, a 21-year-old Swede studying at Edinburgh University. This is her first time and she's here on a recommendation from friends – and because her mum searched her luggage before she left Stockholm and confiscated all the good stuff. That meant she arrived with just five vinyl LPs.

"My mum wouldn't let me take the Led Zeppelin and a few other things," she says. "I do download music sometimes and I have an iPod. But if it's an artist I really do like I'll go and buy the record because I want to support them. Someone like Tom Waits I would want to have either on LP or on CD."

What she's clutching right now is an old seven-inch single by Sammy Davis Jr. She's also found vinyl copies of Paul Simon's Still Crazy After All These Years and a Shirley Bassey compilation. "I've got that," I tell her. "Is it good?" she asks. "It's not bad," I say, knowingly. "Goldfinger's good." I don't think I stroked my chin, but I might have.

For 36-year-old librarian Ed Jupp, this is the sort of exchange that can still turn a record shop into holy ground, a cloistered space walled in by records and CDs where communities of music lovers gather. Or worship, to continue the religious metaphor.

Jupp, funnily enough, used to teach religious education. He also used to work for HMV and Fopp – "as a till monkey" – and writes a music blog, 17 Seconds. "It's about meeting like-minded individuals. It's also about the element of chance," he says when I ask him to lay out the joys of the record shop. "You can have online communities but there's nothing like meeting people in real life, exchanging tapes, having stuff recommended. I don't think I've ever seen the internet being able to replicate that, though I'm aware that could be a generational thing – you could talk to a 14-year-old who would think I was from another planet."

Standing nearby are a couple of 17-year-olds who probably don't think that. Hannah Nugent and Becca Tarrant are at Currie High School in Edinburgh. Nugent is a "huge" Green Day fan and as we talk she casts covetous looks at a poster of the band on the wall. "A lot of people my age don't go to record shops because they prefer to download stuff, but I prefer CDs." She says she normally shops in HMV but "we were in a clothes shop nearby and this looked awesome so we came in. It's cool".

For a retail sector desperate for new customers, people like Nugent, Tarrant, Berten and Fristorp – all young women, note – are great news. They are not intimidated, and they don't see the record shop as an anachronism. Although the title of Graham Jones's documentary implies there may soon be even less than Kevin Buckle's apocalyptic half-dozen stores still standing, its subtitle tells a different story. It reads: "The Rise, Fall And Rebirth Of The Independent Record Shop."

There is still life here. More than that, there's hope too – and perhaps the best evidence comes from the mouths of our two young friends, the ones for whom even Britpop was an obscure, pre-natal event.

"I like it," laughs James Byron, the digital native, taking in the atmosphere and looking around at the clutter of posters and albums. "It's what my bedroom's like."

And maybe it's that solitary fact which will save the day.