How to download audio from a video link in Python

Viewed 656

I have a list of links that I am iterating through such as below

https://www.loc.gov/item/2015669100/
https://www.loc.gov/item/2015669101/
https://www.loc.gov/item/2015669102/
https://www.loc.gov/item/2015669103/
https://www.loc.gov/item/2015669104/
https://www.loc.gov/item/2015669105/
https://www.loc.gov/item/2015669106/
https://www.loc.gov/item/2015669107/
https://www.loc.gov/item/2015669108/
https://www.loc.gov/item/2015669109/

If you look at these links, you can see that it has a video and a downloadable XML file. My task is to download the audio from the video and download the XML file together from one page.

My problem is that, how do I just get the audio from such audio files?

below is my code currently.

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen

base_html = "https://www.loc.gov/collections/civil-rights-history-project/?sp={}"

for i in range(1,8):
    html = base_html.format(i)
    req = Request(html, headers={'User-Agent': 'Mozilla/5.0'})
    soup = BeautifulSoup(urlopen(req).read(), 'html.parser')
    
    pages = soup.findAll('div', attrs={'class' : 'item-description'})
    for div in pages:
            crawl_p = div.find('a')['href']
            #some logic here

1 Answers

Looking at the site, it looks like the video and audio files streamed through transport segments(.ts files) form m3u8 url.

The <source> contains the url for the m3u8 stream. The tag is identified using its attributes in the tag.(Here it is type attribute).

import subprocess
from bs4 import BeautifulSoup
import requests as r

sess = r.session()
site_url = "https://www.loc.gov/item/2015669100/"
request = sess.get(site_url)
#print(request.content)

soup = BeautifulSoup(request.content, 'html5lib')
m3u8_url = soup.find('source',attrs={'type' : 'application/x-mpegURL'})['src']
print(str(m3u8_url))

This scraps the m3u8 url, https://tile.loc.gov/streaming-services/iiif/service:afc:afc2010039:afc2010039_crhp0001:afc2010039_crhp0001_mv04/full/full/0/full/default.m3u8

ffmpeg can download the streams(video or audio) from m3u8 files. It can also be run from python.

subprocess.call(['ffmpeg','-i',m3u8_url,'-vn','-map','a','output.ts' ])

Don't forget to wrap the command with []. Each word inside quotes represents a space separated command. The complete ffmpeg command is ffmpeg -i m3u8_url -vn -map a output.ts

Here is the complete code. Just make sure ffmpeg path variable is included, else subprocess will throw error. The file size is large, so it might take a while to download the audio file.

import subprocess
from bs4 import BeautifulSoup
import requests as r

sess = r.session()
site_url = "https://www.loc.gov/item/2015669100/"
request = sess.get(site_url)
#print(request.content)

soup = BeautifulSoup(request.content, 'html5lib')
m3u8_url = soup.find('source',attrs={'type' : 'application/x-mpegURL'})['src']
print(str(m3u8_url))

subprocess.call(['ffmpeg','-i',m3u8_url,'-vn','-map','a','output.ts' ])
Related