3 #=======================================================================
5 # File ID: c5f8c456-f24a-11e2-a3f1-001f3b596ec9
7 # Sort videos downloaded by youtube-dl / yt-dlp in chronological order
9 # Author: Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later.
11 #=======================================================================
39 " -n "$progname" -- "$@
")"
40 test "$?" = "0" ||
exit 1
47 opt_orderby
=upload_date
,file
55 -d|
--dbname) opt_dbname
="$2"; shift 2 ;;
56 -f|
--filenames) opt_filenames
=1; shift ;;
57 -F|
--from-file) opt_from_file
="$2"; shift 2 ;;
58 -h|
--help) opt_help
=1; shift ;;
59 -o|
--order-by) opt_orderby
="$2"; shift 2 ;;
60 -q|
--quiet) opt_quiet
=$
(($opt_quiet + 1)); shift ;;
61 -r|
--reuse) opt_reuse
=1; shift ;;
62 -S|
--sql) opt_sql
="$2"; shift 2 ;;
63 -v|
--verbose) opt_verbose
=$
(($opt_verbose + 1)); shift ;;
64 -w|
--where) opt_where
="$2"; shift 2 ;;
65 --version) echo $progname $VERSION; exit 0 ;;
67 *) echo $progname: Internal error
>&2; exit 1 ;;
70 opt_verbose
=$
(($opt_verbose - $opt_quiet))
72 if test "$opt_help" = "1"; then
73 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
76 Sort videos downloaded by youtube-dl / yt-dlp in chronological order
78 Usage: $progname [options]
82 -d FILE, --dbname FILE
83 Use FILE as database instead of storing it under
84 ~/.cache/$progname/[DIR].
86 List filenames instead of the default format with date and title.
87 Useful for creating playlists.
88 -F FILE, --from-file FILE
89 Read filenames from FILE instead of using find(1).
92 -o COLUMN, --order-by COLUMN
93 Sort output by COLUMN.
95 Be more quiet. Can be repeated to increase silence.
97 Reuse the database for the current directory, don't scan files and
99 -S STATEMENT, --sql STATEMENT
100 Execute SQL statement directly against the sqlite3 database. If
101 STATEMENT is "-", read commands from stdin or if stdin is empty, use
102 the sqlite3 shell. Examples:
104 $progname -S "SELECT view_count AS v, file FROM yt ORDER BY v;"
106 (echo .dbinfo; echo "SELECT file FROM yt;") | $progname -r -S -
108 Increase level of verbosity. Can be repeated.
110 Print version information.
111 -w CLAUSE, --where CLAUSE
112 List only entries specified by CLAUSE.
120 sed "s/'/''/g; s/.*/INSERT INTO $1 VALUES('&');/;"
124 if test -n "$opt_dbname"; then
127 odir
="$HOME/.cache/$progname$(pwd -P)"
128 mkdir
-p "$odir" ||
exit 1
129 db
="$odir/$progname.sqlite"
131 if test "$opt_reuse" != "1" -o ! -e "$db"; then
132 rm -f "$db" "$db-journal"
133 cat <<SQL_END | sqlite3 "$db" || exit 1
134 CREATE TABLE t (file TEXT);
139 dislike_count INTEGER,
163 if test -n "$opt_from_file"; then
164 grep '\.info\.json$' "$opt_from_file"
166 find -name '*.info.json'
167 fi | mkinsert t | sqlite3
"$db"
168 cat <<SQL_END | sqlite3 "$db"
170 ALTER TABLE t ADD j TEXT;
171 UPDATE t SET j = json(readfile(file));
174 json_extract(j, '$.average_rating') AS average_rating,
175 json_extract(j, '$.description') AS description,
177 json_extract(j, '$.dislike_count') AS dislike_count,
178 json_extract(j, '$.display_id') AS display_id,
179 json_extract(j, '$.duration') AS duration,
180 json_extract(j, '$.extractor') AS extractor,
182 json_extract(j, '$.filesize') AS filesize,
183 json_extract(j, '$.format') AS format,
184 json_extract(j, '$.format_id') AS format_id,
185 json_extract(j, '$.fulltitle') AS fulltitle,
186 json_extract(j, '$.height') AS height,
188 json_extract(j, '$.like_count') AS like_count,
189 json_extract(j, '$.width') || 'x' || json_extract(j, '$.height')
191 json_extract(j, '$.title') AS title,
192 json_extract(j, '$.upload_date') AS upload_date,
193 json_extract(j, '$.uploader') AS uploader,
194 json_extract(j, '$.uploader_id') AS uploader_id,
195 json_extract(j, '$.view_count') AS view_count,
196 json_extract(j, '$.webpage_url') AS webpage_url,
197 json_extract(j, '$.width') AS width,
198 json_extract(j, '$.id') AS ytid,
201 UPDATE yt SET upload_date = substr(upload_date, 1, 4) || '-'
202 || substr(upload_date, 5, 2) || '-' || substr(upload_date, 7, 2);
203 UPDATE yt SET length = printf('%02u:%02u:%02u',
211 if test -n "$opt_sql"; then
212 if test "$opt_sql" = "-"; then
215 sqlite3
"$db" "$opt_sql" |
$PAGER
220 where_str
='WHERE ytid IS NOT NULL'
221 if test -n "$opt_where"; then
222 where_str
="WHERE ($opt_where) AND ytid IS NOT NULL"
225 if test "$opt_filenames" = "1"; then
226 sqlite3
"$db" "DROP TABLE IF EXISTS films;"
227 sqlite3
"$db" "CREATE TABLE films (file TEXT);"
228 find | filmer | mkinsert films | sqlite3
"$db"
229 cat <<SQL_END | sqlite3 "$db" | sqlite3 "$db"
231 SELECT printf('SELECT file FROM films WHERE file LIKE ''%%-%s%%'';', ytid)
234 ORDER BY $opt_orderby;
238 cat <<SQL_END | sqlite3 -header -column "$db" | sed 's/ *$//g;' | $PAGER
247 ORDER BY $opt_orderby;