Skip to content

Commit

Permalink
Switch to the description from the API response
Browse files Browse the repository at this point in the history
This is necessary to workaround the extraction bug currently present
in youtube-dl (ytdl-org/youtube-dl#25937)
  • Loading branch information
alexmerkel committed Jul 13, 2020
1 parent 2481255 commit 5821ca4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
8 changes: 8 additions & 0 deletions exiftool.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%Image::ExifTool::UserDefined = (
'Image::ExifTool::QuickTime::ItemList' => {
"\xa9des" => { Name => 'Description', Avoid => 1 },
dscp => { Name => 'Description', Avoid => 1 },
desc => { Name => 'Description' },
},
);
1; #end
10 changes: 6 additions & 4 deletions ytameta.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def addMetadata(args):
#Get video filepath
youtubeID = item[0]
try:
[timestamp, duration, tags] = getMetadata(youtubeID)
[timestamp, duration, tags, _] = getMetadata(youtubeID)
db.execute("UPDATE videos SET timestamp = ?, duration = ?, tags = ? WHERE youtubeID = ?", (timestamp, duration, tags, youtubeID))
except FileNotFoundError:
print("WARNING: No Youtube data API key available, unable to load additional metadata")
Expand All @@ -65,7 +65,7 @@ def getMetadata(youtubeID):
:raises: :class:``OSError: Unable to read API key from file
:raises: :class:``requests.exceptions.HTTPError: Unable to get metadata
:returns: List with timestamp (int) at index 0, duration (int) at index 1, and tags (string) at index 2
:returns: List with timestamp (int) at index 0, duration (int) at index 1, tags (string) at index 2, and description (string) at index 3
:rtype: list
'''
#Get API key
Expand All @@ -82,12 +82,14 @@ def getMetadata(youtubeID):
#Check if empty
if not d["items"]:
print("WARNING: No metadata available for " + youtubeID)
return [None, None, None]
return [None, None, None, None]
#Convert update time to timestamp
try:
timestamp = int(datetime.timestamp(datetime.strptime(d["items"][0]["snippet"]["publishedAt"], "%Y-%m-%dT%H:%M:%S.%f%z")))
except ValueError:
timestamp = int(datetime.timestamp(datetime.strptime(d["items"][0]["snippet"]["publishedAt"], "%Y-%m-%dT%H:%M:%S%z")))
#Get description
description = d["items"][0]["snippet"]["description"]
#Convert duration to seconds
duration = convertDuration(d["items"][0]["contentDetails"]["duration"])
#Extract tags
Expand All @@ -96,7 +98,7 @@ def getMetadata(youtubeID):
else:
tags = None
#Return results
return [timestamp, duration, tags]
return [timestamp, duration, tags, description]
# ########################################################################### #

# --------------------------------------------------------------------------- #
Expand Down
9 changes: 8 additions & 1 deletion ytapost.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def processFile(name, subLang, db, check):
duration = None
tags = None
try:
[timestamp, duration, tags] = ytameta.getMetadata(videoID)
[timestamp, duration, tags, apiDesc] = ytameta.getMetadata(videoID)
except FileNotFoundError:
print("WARNING: No Youtube data API key available, unable to load additional metadata")
except OSError:
Expand Down Expand Up @@ -165,6 +165,13 @@ def processFile(name, subLang, db, check):
cmd = ["exiftool", "-api", "largefilesupport=1", "-m", "--printConv", "-overwrite_original", "-HDVideo={}".format(hd), newName]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
process.wait()
#Use description from API if available
if apiDesc:
desc = apiDesc
config = os.path.join(os.path.dirname(os.path.realpath(__file__)), "exiftool.config")
cmd = ["exiftool", "-config", config, "-api", "largefilesupport=1", "-overwrite_original", "-ec", "-Description={}".format(desc), newName]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
process.wait()
#Check if fix requried
artist, title = ytafix.fixVideo(newName, videoID, fileArtist=artist)
#Calculate checksum
Expand Down

0 comments on commit 5821ca4

Please sign in to comment.