Skip to content

Commit

Permalink
Merge pull request #2 from vpetersson/duration
Browse files Browse the repository at this point in the history
Adds duration
  • Loading branch information
vpetersson committed Dec 30, 2023
2 parents 041d304 + 8ff3e14 commit ba7a8e8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
pip install -r requirements.txt
pip install flake8
pip install yamllint
sudo apt-get update && sudo apt-get install ffmpeg
- name: Lint with flake8
run: |
Expand All @@ -37,6 +38,7 @@ jobs:
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Ensure example file passes YAmllint
run: |
yamllint -fgithub -d "{rules: {line-length: false}}" podcast_config.example.yaml
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This tool was written for my podcast [Nerding Out with Viktor](https://blog.vikt

- Python 3.8 or higher
- pip (Python package installer)
- ffmpeg

### Setup

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
PyYAML==6.0.1
requests==2.31.0
Markdown==3.5.1
sh==2.0.6
35 changes: 34 additions & 1 deletion rss_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import markdown
import requests
import yaml
from sh import ffprobe


def read_podcast_config(yaml_file_path):
Expand All @@ -19,9 +20,37 @@ def convert_iso_to_rfc2822(iso_date):

def get_file_info(url):
response = requests.head(url, allow_redirects=True)

# Get duration of audio/video file
# We're using the response.url here in order to
# follow redirects and get the actual file

probe = ffprobe(
"-hide_banner",
"-v",
"quiet",
"-show_streams",
"-print_format",
"flat",
response.url,
)
lines = probe.split("\n")

# Filtering out the line that contains 'streams.stream.0.duration'
duration_line = next(
(line for line in lines if line.startswith("streams.stream.0.duration=")), None
)

if duration_line:
# Extracting the numeric value and converting it to an integer
duration = int(float(duration_line.split("=")[1].strip('"')))
else:
duration = None

return {
"content-length": response.headers.get("content-length"),
"content-type": response.headers.get("content-type"),
"duration": duration,
}


Expand Down Expand Up @@ -93,7 +122,7 @@ def generate_rss(config, output_file_path):
itunes_author = ET.SubElement(channel, "itunes:author")
itunes_author.text = metadata["itunes_author"]

# Duplicate descrion to itunes summary
# Duplicate description to itunes summary
itunes_summary = ET.SubElement(channel, "itunes:summary")
itunes_summary.text = metadata["description"]

Expand Down Expand Up @@ -138,6 +167,10 @@ def generate_rss(config, output_file_path):
itunes_explicit = ET.SubElement(item, "itunes:explicit")
itunes_explicit.text = global_explicit

# Add itunes:duration tag
itunes_duration = ET.SubElement(item, "itunes:duration")
itunes_duration.text = str(file_info["duration"])

# New iTunes-specific tags
if "episode" in episode:
itunes_episode = ET.SubElement(item, "itunes:episode")
Expand Down

0 comments on commit ba7a8e8

Please sign in to comment.