1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
|
#!/usr/bin/env python3
"""
ytm β CLI YouTube Music Player
Search YouTube, queue and play audio through mpv.
All operations are non-interactive shell commands.
Usage:
ytm tui Launch interactive TUI (text user interface)
ytm search <query> Search YouTube and print numbered results
ytm play [n|url|id] Play search result #n (default 1), or a URL/ID
ytm mpv [n|url|id] Open video in mpv window for full HD
ytm add <n> [n ...] Add search result(s) #n to the queue
ytm add-id <url|id> Add a specific video by URL/ID to the queue
ytm remove <n> Remove item #n from the queue
ytm next Skip to next track in queue
ytm prev Previous track
ytm stop Stop playback and clear queue
ytm pause Toggle play/pause
ytm queue Show current queue and now-playing
ytm clear Clear the queue
ytm volume [0-100] Get or set volume
ytm loop Toggle queue looping
ytm status Show now-playing and player state
ytm help Show this help
ytm <query> Shorthand: search + interactive pick (fzf)
Examples:
ytm tui
ytm search akon
ytm play 2
ytm add 1 2 3
ytm remove 2
ytm add-id dQw4w9WgXcQ
ytm next
"""
import json
import os
import shutil
import socket
import subprocess
import sys
import textwrap
import time
import urllib.request
from pathlib import Path
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
CACHE_DIR = Path.home() / ".cache" / "ytm"
STATE_FILE = CACHE_DIR / "state.json"
MPV_SOCKET = "/tmp/ytm-mpv-socket"
YT_DLP = shutil.which("yt-dlp") or "yt-dlp"
MPV = shutil.which("mpv") or "mpv"
FZF = shutil.which("fzf")
DEFAULT_STATE = {
"queue": [],
"current_index": -1,
"last_search": [],
"volume": 70,
"loop": False,
}
# ---------------------------------------------------------------------------
# State
# ---------------------------------------------------------------------------
# ββ ANSI 16-color palette for thumbnail rendering ββ
_ANSI_RGB = [
(0x00, 0x00, 0x00), # 0 black
(0xAA, 0x00, 0x00), # 1 red
(0x00, 0xAA, 0x00), # 2 green
(0xAA, 0x55, 0x00), # 3 brown/yellow
(0x00, 0x00, 0xAA), # 4 blue
(0xAA, 0x00, 0xAA), # 5 magenta
(0x00, 0xAA, 0xAA), # 6 cyan
(0xAA, 0xAA, 0xAA), # 7 light gray
(0x55, 0x55, 0x55), # 8 dark gray
(0xFF, 0x55, 0x55), # 9 bright red
(0x55, 0xFF, 0x55), # 10 bright green
(0xFF, 0xFF, 0x55), # 11 bright yellow
(0x55, 0x55, 0xFF), # 12 bright blue
(0xFF, 0x55, 0xFF), # 13 bright magenta
(0x55, 0xFF, 0xFF), # 14 bright cyan
(0xFF, 0xFF, 0xFF), # 15 bright white
]
def _rgb_to_ansi(r: int, g: int, b: int) -> int:
"""Map (r,g,b) to the nearest ANSI 16-color index."""
best = 0
best_dist = float("inf")
for i, (cr, cg, cb) in enumerate(_ANSI_RGB):
dr = r - cr
dg = g - cg
db = b - cb
dist = dr * dr + dg * dg + db * db
if dist < best_dist:
best = i
best_dist = dist
return best
def _get_thumbnail(video_id: str) -> str | None:
"""Download YouTube thumbnail to cache. Returns local path or None."""
thumb_dir = CACHE_DIR / "thumbs"
thumb_dir.mkdir(parents=True, exist_ok=True)
thumb_path = thumb_dir / f"{video_id}.jpg"
if thumb_path.exists():
return str(thumb_path)
url = f"https://img.youtube.com/vi/{video_id}/hqdefault.jpg"
try:
urllib.request.urlretrieve(url, thumb_path)
return str(thumb_path)
except Exception:
return None
def _ensure_cache() -> None:
CACHE_DIR.mkdir(parents=True, exist_ok=True)
def load_state() -> dict:
_ensure_cache()
if STATE_FILE.exists():
try:
return {**DEFAULT_STATE, **json.loads(STATE_FILE.read_text())}
except (json.JSONDecodeError, TypeError):
pass
return dict(DEFAULT_STATE)
def save_state(state: dict) -> None:
_ensure_cache()
STATE_FILE.write_text(json.dumps(state, indent=2, default=str))
# ---------------------------------------------------------------------------
# YouTube helpers
# ---------------------------------------------------------------------------
def search_yt(query: str, limit: int = 15) -> list[dict]:
"""Search YouTube. Returns list of {id, title, duration, channel, url}."""
cmd = [
YT_DLP, "--flat-playlist", "--dump-single-json",
"--no-playlist",
"--default-search", "ytsearch",
"-f", "bestaudio/best",
f"ytsearch{limit}:{query}",
]
try:
r = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if r.returncode != 0:
_eprint(f"Search error: {r.stderr.strip()}")
return []
data = json.loads(r.stdout)
entries = data if isinstance(data, list) else data.get("entries", [])
return [
{
"id": e["id"],
"title": e.get("title", "Unknown"),
"duration": e.get("duration") or 0,
"channel": e.get("channel") or e.get("uploader", "Unknown"),
"url": f"https://youtube.com/watch?v={e['id']}",
}
for e in entries if e.get("id")
]
except subprocess.TimeoutExpired:
_eprint("Search timed out.")
except (json.JSONDecodeError, KeyError) as exc:
_eprint(f"Search parse error: {exc}")
return []
def get_metadata(video_id: str) -> dict | None:
"""Fetch full metadata for a video URL or ID."""
url = video_id if video_id.startswith("http") else f"https://youtube.com/watch?v={video_id}"
cmd = [YT_DLP, "--dump-single-json", "--no-playlist", url]
try:
r = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if r.returncode != 0:
return None
d = json.loads(r.stdout)
return {
"id": d.get("id", ""),
"title": d.get("title", "Unknown"),
"duration": d.get("duration") or 0,
"channel": d.get("channel") or d.get("uploader", "Unknown"),
"url": d.get("webpage_url", url),
}
except (subprocess.TimeoutExpired, json.JSONDecodeError):
return None
def get_audio_url(video_url: str) -> str | None:
"""Extract the best audio stream URL."""
cmd = [YT_DLP, "-f", "bestaudio/best", "--get-url", video_url]
try:
r = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if r.returncode == 0:
return r.stdout.strip().split("\n")[0]
except subprocess.TimeoutExpired:
pass
return None
def get_related(video_id: str, limit: int = 20) -> list[dict]:
"""Fetch related/recommended videos via YouTube Mix radio playlist.
Returns up to *limit* items (excluding the seed video).
Items have the same {id, title, duration, channel, url} format as search results.
Falls back to an empty list on any error.
"""
mix_url = f"https://www.youtube.com/watch?v={video_id}&list=RD{video_id}"
cmd = [
YT_DLP, "--flat-playlist", "--dump-single-json",
"--no-download", "--no-warnings", "--quiet",
mix_url,
]
try:
r = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if r.returncode != 0:
return []
data = json.loads(r.stdout)
entries = data if isinstance(data, list) else data.get("entries", [])
related = []
for e in entries:
eid = e.get("id")
if not eid or eid == video_id:
continue # skip the seed video
related.append({
"id": eid,
"title": e.get("title", "Unknown"),
"duration": e.get("duration") or 0,
"channel": e.get("channel") or e.get("uploader", "Unknown"),
"url": f"https://youtube.com/watch?v={eid}",
})
if len(related) >= limit:
break
return related
except (subprocess.TimeoutExpired, json.JSONDecodeError, KeyError):
return []
# ---------------------------------------------------------------------------
# Formatting
# ---------------------------------------------------------------------------
def fmt_dur(seconds: int | float) -> str:
if not seconds:
return "--:--"
m, s = divmod(int(seconds), 60)
h, m = divmod(m, 60)
return f"{h}:{m:02d}:{s:02d}" if h else f"{m}:{s:02d}"
def fmt_item(item: dict) -> str:
dur = fmt_dur(item.get("duration", 0))
return f"{item['title']} [{dur}] ({item['channel']})"
# ---------------------------------------------------------------------------
# MPV IPC
# ---------------------------------------------------------------------------
def _mpv_send(cmd: dict) -> dict | None:
"""Send JSON command to mpv via Unix socket."""
try:
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.settimeout(3)
s.connect(MPV_SOCKET)
s.sendall(json.dumps(cmd).encode() + b"\n")
resp = b""
while True:
chunk = s.recv(4096)
if not chunk:
break
resp += chunk
if b"\n" in chunk:
break
s.close()
if resp:
return json.loads(resp.decode().strip())
except (OSError, json.JSONDecodeError):
return None
return None
def mpv_running() -> bool:
"""Check if mpv is actually running by testing the IPC socket."""
if not os.path.exists(MPV_SOCKET):
return False
resp = _mpv_send({"command": ["get_property", "pid"]})
return resp is not None and resp.get("error") in (None, "success")
def _wait_for_socket(timeout: float = 4.0) -> bool:
"""Wait for the mpv IPC socket file to appear (mpv is starting)."""
for _ in range(int(timeout / 0.25)):
if os.path.exists(MPV_SOCKET):
return True
time.sleep(0.25)
return False
def start_mpv(url: str, volume: int = 70, paused: bool = False) -> bool:
"""Start or reload mpv for playback."""
if mpv_running():
resp = _mpv_send({"command": ["loadfile", url, "replace"]})
if resp and resp.get("error") in (None, "success"):
_mpv_send({"command": ["set_property", "volume", volume]})
return True
# Stale socket β quit mpv and start fresh
_mpv_send({"command": ["quit"]})
time.sleep(0.3)
try:
os.unlink(MPV_SOCKET)
except OSError:
pass
# Clean any leftover socket before fresh start
try:
os.unlink(MPV_SOCKET)
except OSError:
pass
cmd = [
MPV,
"--no-video",
f"--volume={volume}",
f"--input-ipc-server={MPV_SOCKET}",
"--no-terminal",
url,
]
if paused:
cmd.insert(1, "--pause")
subprocess.Popen(
cmd,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
preexec_fn=os.setsid,
)
if not _wait_for_socket():
_eprint("mpv failed to start (IPC socket not ready).")
return False
return True
def stop_mpv() -> None:
if mpv_running():
_mpv_send({"command": ["quit"]})
try:
os.unlink(MPV_SOCKET)
except OSError:
pass
def start_mpv_video(url: str) -> None:
"""Open a YouTube video in mpv window for full HD playback.
Uses mpv's built-in yt-dlp support to resolve and play the best
quality video stream. Starts at 60% of screen size with a
decorated, freely resizable window.
"""
subprocess.Popen(
[MPV, "--no-terminal", "--autofit=60%", "--border", "--quiet", url],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
preexec_fn=os.setsid,
)
def mpv_get(prop: str):
resp = _mpv_send({"command": ["get_property", prop]})
if resp and "data" in resp:
return resp["data"]
return None
# ---------------------------------------------------------------------------
# Playback
# ---------------------------------------------------------------------------
def _play(item: dict, state: dict) -> bool:
"""Resolve audio URL and start playing the given item."""
print(f"βΆ {item['title']}")
url = get_audio_url(item["url"])
if not url:
_eprint("Could not extract audio stream.")
return False
if start_mpv(url, volume=state.get("volume", 70)):
state["current"] = item
state["playing"] = True
return True
return False
# ---------------------------------------------------------------------------
# Commands
# ---------------------------------------------------------------------------
def cmd_search(query: str) -> None:
"""Search YouTube, print numbered results, save to state."""
results = search_yt(query)
if not results:
print("No results found.")
return
state = load_state()
state["last_search"] = results
save_state(state)
for i, r in enumerate(results, 1):
print(f" {i:2d}. {fmt_item(r)}")
def cmd_play(n_or_url: str | None = None) -> None:
"""
Play an item by index from last search, or by URL/ID.
Default: play result #1 from last search.
"""
state = load_state()
# If argument looks like a URL or video ID (11 chars alphanumeric)
if n_or_url and (n_or_url.startswith("http") or
(len(n_or_url) in (11, 34) and n_or_url.replace("_", "").isalnum())):
meta = get_metadata(n_or_url)
if not meta:
_eprint("Could not fetch video metadata.")
return
state["queue"] = []
state["current_index"] = -1
_play(meta, state)
save_state(state)
return
# Otherwise play by index from last search
try:
n = int(n_or_url) if n_or_url else 1
except ValueError:
meta = get_metadata(n_or_url)
if meta:
state["queue"] = []
state["current_index"] = -1
_play(meta, state)
save_state(state)
return
_eprint(f"Invalid input: {n_or_url}")
return
results = state.get("last_search", [])
if not results:
_eprint("No search results. Run 'ytm search <query>' first.")
return
if n < 1 or n > len(results):
_eprint(f"Invalid index {n}. Results are 1-{len(results)}.")
return
item = results[n - 1]
state["queue"] = []
state["current_index"] = -1
_play(item, state)
save_state(state)
def cmd_add(n: int) -> None:
"""Add search result #n to the queue."""
state = load_state()
results = state.get("last_search", [])
if not results:
_eprint("No search results. Run 'ytm search <query>' first.")
return
if n < 1 or n > len(results):
_eprint(f"Invalid index {n}. Results are 1-{len(results)}.")
return
item = results[n - 1]
state["queue"].append(item)
print(f"β {item['title']}")
if not mpv_running() and state["current_index"] < 0:
state["current_index"] = len(state["queue"]) - 1
_play(item, state)
save_state(state)
def cmd_add_id(vid: str) -> None:
"""Add a specific video by URL or ID to the queue."""
state = load_state()
meta = get_metadata(vid)
if not meta:
_eprint("Could not fetch video metadata.")
return
state["queue"].append(meta)
print(f"β {meta['title']}")
if not mpv_running() and state["current_index"] < 0:
state["current_index"] = len(state["queue"]) - 1
_play(meta, state)
save_state(state)
def cmd_auto(n_or_url: str | None = None) -> None:
"""Play a song and auto-queue related/recommended songs."""
state = load_state()
# Resolve input to a video item (same resolution logic as cmd_play)
item: dict | None = None
# Case 1: URL or video ID (11 or 34 chars alphanumeric)
if n_or_url and (
n_or_url.startswith("http")
or (len(n_or_url) in (11, 34) and n_or_url.replace("_", "").isalnum())
):
meta = get_metadata(n_or_url)
if meta:
item = meta
# Case 2: numeric index into last_search
if item is None:
try:
n = int(n_or_url) if n_or_url else 1
except ValueError:
meta = get_metadata(n_or_url)
if meta:
item = meta
else:
results = state.get("last_search", [])
if not results:
_eprint("No search results. Run 'ytm search <query>' first.")
return
if n < 1 or n > len(results):
_eprint(f"Invalid index {n}. Results are 1-{len(results)}.")
return
item = results[n - 1]
if item is None:
_eprint("Could not resolve video.")
return
video_id = item["id"]
# Play the seed song
state["queue"] = [item]
state["current_index"] = 0
ok = _play(item, state)
if not ok:
return
# Fetch related songs in background
related = get_related(video_id)
if related:
state["queue"].extend(related)
print(f"π» Auto-queued {len(related)} related song{'s' if len(related) != 1 else ''}")
save_state(state)
def cmd_next() -> None:
state = load_state()
if not state["queue"]:
print("Queue is empty.")
return
nxt = state["current_index"] + 1
if nxt < len(state["queue"]):
state["current_index"] = nxt
elif state.get("loop", False):
state["current_index"] = 0
else:
print("End of queue.")
state.pop("current", None)
state["playing"] = False
save_state(state)
return
_play(state["queue"][state["current_index"]], state)
save_state(state)
def cmd_prev() -> None:
state = load_state()
if not state["queue"] or state["current_index"] <= 0:
print("No previous track.")
return
state["current_index"] -= 1
_play(state["queue"][state["current_index"]], state)
save_state(state)
def cmd_stop() -> None:
stop_mpv()
state = load_state()
state["playing"] = False
state["queue"] = []
state["current_index"] = -1
state.pop("current", None)
save_state(state)
print("βΉ Stopped. Queue cleared.")
def cmd_pause() -> None:
if not mpv_running():
print("Not playing.")
return
resp = _mpv_send({"command": ["cycle", "pause"]})
if resp and resp.get("error") in (None, "success"):
paused = mpv_get("pause")
print("βΈ Paused" if paused else "βΆ Resumed")
else:
_eprint("Could not communicate with mpv.")
def cmd_volume(args: list[str]) -> None:
if not args:
vol = mpv_get("volume")
print(f"π Volume: {vol}%" if vol is not None else "Not playing.")
return
try:
vol = max(0, min(100, int(args[0])))
_mpv_send({"command": ["set_property", "volume", vol]})
state = load_state()
state["volume"] = vol
save_state(state)
print(f"π Volume: {vol}%")
except ValueError:
_eprint("Volume must be 0-100.")
def cmd_queue() -> None:
state = load_state()
current = state.get("current")
if current:
print(f"βΆ {current['title']} [{fmt_dur(current.get('duration', 0))}]")
elif mpv_running():
meta = mpv_get("metadata")
if meta:
print(f"βΆ {meta.get('title', 'Unknown')}")
else:
print("Not playing.")
if state["queue"]:
print(f"\nQueue ({len(state['queue'])} track{'' if len(state['queue']) == 1 else 's'}):")
for i, item in enumerate(state["queue"]):
dur = fmt_dur(item.get("duration", 0))
marker = "βΈ" if i == state["current_index"] else " "
print(f" {marker} {i + 1}. {item['title']} [{dur}]")
paused = mpv_get("pause")
vol = mpv_get("volume")
if paused is not None:
status = "βΈ Paused" if paused else "βΆ Playing"
print(f"\n{status} | π {vol}% | π {'on' if state['loop'] else 'off'}")
else:
print(f"\nπ Loop: {'on' if state['loop'] else 'off'}")
def cmd_clear() -> None:
state = load_state()
state["queue"] = []
state["current_index"] = -1
save_state(state)
print("π Queue cleared.")
def cmd_remove(n: int) -> None:
"""Remove item #n (1-based) from the queue."""
state = load_state()
if not state["queue"]:
_eprint("Queue is empty.")
return
if n < 1 or n > len(state["queue"]):
_eprint(f"Invalid index {n}. Queue has {len(state['queue'])} items.")
return
idx = n - 1
removed = state["queue"].pop(idx)
print(f"β Removed: {removed['title']}")
# Adjust current_index if it pointed to or past the removed item
if state["current_index"] >= len(state["queue"]):
state["current_index"] = len(state["queue"]) - 1
elif idx < state["current_index"]:
state["current_index"] -= 1
# If we removed the currently playing item, stop and play next if available
if idx == state["current_index"] or (state.get("current") and
state["current"].get("id") == removed.get("id")):
if state["queue"] and state["current_index"] >= 0:
_play(state["queue"][state["current_index"]], state)
else:
stop_mpv()
state.pop("current", None)
state["playing"] = False
state["current_index"] = -1
save_state(state)
def cmd_loop() -> None:
state = load_state()
state["loop"] = not state["loop"]
save_state(state)
print(f"π Loop: {'on' if state['loop'] else 'off'}")
def cmd_status() -> None:
state = load_state()
if not mpv_running():
print("No active playback.")
if state["queue"]:
print(f"Queue: {len(state['queue'])} track{'' if len(state['queue']) == 1 else 's'} waiting.")
return
paused = mpv_get("pause")
vol = mpv_get("volume")
time_pos = mpv_get("time-pos")
duration = mpv_get("duration")
path_meta = mpv_get("metadata")
current = state.get("current", {})
title = current.get("title", path_meta.get("title", "Unknown") if path_meta else "Unknown")
channel = current.get("channel", path_meta.get("artist", "") if path_meta else "")
print(f"π΅ {title}")
if channel:
print(f" {channel}")
if time_pos is not None:
print(f"β± {fmt_dur(time_pos)} / {fmt_dur(duration)}")
print(f"π Volume: {vol}%")
status_str = "βΈ Paused" if paused else "βΆ Playing"
print(f"Status: {status_str}")
print(f"π Loop: {'on' if state['loop'] else 'off'}")
if state["queue"]:
remaining = len(state["queue"]) - state["current_index"] - 1
print(f"π {remaining} track{'' if remaining == 1 else 's'} remaining")
# ---------------------------------------------------------------------------
# Terminal video playback (full-screen ANSI)
# ---------------------------------------------------------------------------
def _render_rgb24_to_ansi(buf: bytes, pw: int, ph: int,
antialias: int = 1) -> list[str]:
"""Render raw rgb24 frame to half-block ANSI strings (one per terminal row).
Each terminal cell is the upper half-block 'β' where foreground = top pixel,
background = bottom pixel, doubling vertical resolution. ANSI color codes
are coalesced (skipped when the same as the previous pixel) to reduce
terminal output size significantly.
When antialias > 1, the video is scaled to pw = term.columns * antialias
and each character cell averages antialias pixel columns for smooth
sub-pixel horizontal rendering.
"""
stride = pw * 3
out_cols = pw // antialias
lines = []
for ty in range(ph // 2):
top_off = ty * 2 * stride
bot_off = (ty * 2 + 1) * stride
cells: list[str] = []
prev = ""
for tx in range(out_cols):
base = tx * antialias * 3
r1_sum = g1_sum = b1_sum = 0
r2_sum = g2_sum = b2_sum = 0
for sx in range(antialias):
o = base + sx * 3
r1_sum += buf[top_off + o]
g1_sum += buf[top_off + o + 1]
b1_sum += buf[top_off + o + 2]
r2_sum += buf[bot_off + o]
g2_sum += buf[bot_off + o + 1]
b2_sum += buf[bot_off + o + 2]
r1 = r1_sum // antialias
g1 = g1_sum // antialias
b1 = b1_sum // antialias
r2 = r2_sum // antialias
g2 = g2_sum // antialias
b2 = b2_sum // antialias
code = f"38;2;{r1};{g1};{b1};48;2;{r2};{g2};{b2}"
if code != prev:
cells.append(f"\033[{code}m")
prev = code
cells.append("β")
cells.append("\033[0m")
lines.append("".join(cells))
return lines
def _write_ansi_frame(lines: list[str], prev_lines: list[str] | None,
term_h: int, elapsed: float, force: bool = False) -> list[str]:
"""Write frame to terminal with line-level differencing.
Only rows that changed between frames are redrawn (via ANSI cursor
positioning), which dramatically reduces output data on near-static scenes.
Returns the current lines for use as prev_lines next frame.
"""
mm, ss = divmod(int(elapsed), 60)
status = f"\033[{term_h};1H\033[K\033[2m[q] quit [Space] pause {mm}:{ss:02d}\033[0m"
if force or prev_lines is None or len(prev_lines) != len(lines):
# Full redraw: clear screen, write all lines
out = ["\033[2J\033[H"]
for i, line in enumerate(lines):
out.append(line)
if i < len(lines) - 1:
out.append("\n")
out.append(status)
sys.stdout.write("".join(out))
else:
# Partial: only changed rows
parts: list[str] = []
for i, (line, old) in enumerate(zip(lines, prev_lines)):
if line != old:
parts.append(f"\033[{i + 1};1H{line}\033[0m\033[K")
if parts:
parts.append(status)
sys.stdout.write("".join(parts))
sys.stdout.flush()
return lines
def _terminal_video(url: str) -> None:
"""Watch a YouTube video in-terminal using 24-bit ANSI true color rendering.
Streams video via direct ffmpeg URL (no yt-dlp pipe), audio via ffplay URL.
Wall-clock sync keeps A/V aligned by dropping video frames when behind.
Frame differencing and ANSI color coalescing minimize terminal output.
No disk writes. Requires: ffmpeg, yt-dlp.
"""
if not sys.stdin.isatty():
_eprint("A terminal with true color support is required for video mode.")
return
ffplay_bin = shutil.which("ffplay")
if not ffplay_bin:
_eprint("Missing dependency: ffplay (from ffmpeg). Install: pacman -S ffmpeg")
return
ytdlp = shutil.which("yt-dlp")
if not ytdlp:
_eprint("\nMissing dependency: yt-dlp. Install: pacman -S yt-dlp")
return
ffmpeg_bin = shutil.which("ffmpeg")
if not ffmpeg_bin:
_eprint("Missing dependency: ffmpeg. Install: pacman -S ffmpeg")
return
# No codec exclusion β ffmpeg handles AV1, VP9, H.264, etc.
format_spec = "bestvideo[height<=1080]+bestaudio/best[height<=1080]"
# --- Step 1: Resolve metadata + extract direct stream URLs ---
print("Resolvingβ¦", end="", flush=True)
try:
r = subprocess.run(
[ytdlp, "--no-warnings", "--no-playlist", "-j", "-f", format_spec, url],
capture_output=True, text=True, timeout=30,
)
if r.returncode != 0:
_eprint(f"\nFailed to resolve: {r.stderr.strip()}")
return
data = json.loads(r.stdout)
except subprocess.TimeoutExpired:
_eprint("\nTimed out resolving video.")
return
except json.JSONDecodeError:
_eprint("\nFailed to parse video info.")
return
width = data.get("width") or 1280
height = data.get("height") or 720
source_fps = data.get("fps", 30) or 30
target_fps = max(15, min(60, int(source_fps)))
# Extract direct stream URLs from requested_formats (separate v/a)
video_url: str | None = None
audio_url: str | None = None
requested = data.get("requested_formats")
if requested:
for fmt in requested:
furl = fmt.get("url")
if not furl:
continue
vcodec = fmt.get("vcodec", "none")
acodec = fmt.get("acodec", "none")
if vcodec not in ("none", None):
video_url = furl
width = fmt.get("width") or width
height = fmt.get("height") or height
if acodec not in ("none", None):
audio_url = furl
else:
# Single muxed stream
video_url = data.get("url")
audio_url = data.get("url")
if not video_url:
_eprint("\nNo playable video stream found.")
return
# --- Step 2: Terminal size for ffmpeg resize ---
term = shutil.get_terminal_size()
ANTIALIAS = 3 # oversample 3x horizontally, average per cell
pw = term.columns * ANTIALIAS # pixel width (ANTIALIAS pixel cols per char cell)
ph = (term.lines - 1) * 2 # pixel height (2 pixel rows per cell, -1 status)
frame_size = pw * ph * 3 # raw rgb24
print(" streamingβ¦", end="", flush=True)
# --- Step 3: Start video decoder (ffmpeg reads URL directly) ---
ffmpeg_proc = subprocess.Popen(
[ffmpeg_bin, "-loglevel", "quiet",
"-i", video_url,
"-vf", f"scale={pw}:{ph},fps={target_fps}",
"-f", "rawvideo", "-pix_fmt", "rgb24",
"-"],
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
)
# --- Step 4: Start audio (ffplay reads audio URL directly) ---
audio_proc: subprocess.Popen | None = None
if audio_url:
audio_proc = subprocess.Popen(
[ffplay_bin, "-nodisp", "-autoexit", "-loglevel", "quiet",
"-i", audio_url],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)
print(" renderingβ¦")
# --- Step 5: Enter full-screen ANSI mode ---
sys.stdout.write("\033[?1049h\033[?25l")
sys.stdout.flush()
import termios
import tty
import select
fd = sys.stdin.fileno()
old_attr = termios.tcgetattr(fd)
try:
tty.setraw(fd)
paused = False
play_elapsed = 0.0 # wall-clock playback position (seconds)
last_clock = time.perf_counter()
frame_idx = 0
prev_lines: list[str] | None = None
last_buf: bytes | None = None
# Prime first frame
first = ffmpeg_proc.stdout.read(frame_size)
if first and len(first) == frame_size:
last_buf = first
frame_idx = 1
lines = _render_rgb24_to_ansi(first, pw, ph, antialias=ANTIALIAS)
prev_lines = _write_ansi_frame(lines, None, term.lines, 0.0, force=True)
while True:
now = time.perf_counter()
dt = now - last_clock
last_clock = now
# --- Non-blocking keyboard ---
if select.select([sys.stdin], [], [], 0)[0]:
ch = sys.stdin.read(1)
if ch == "q":
break
elif ch == " ":
paused = not paused
if paused and audio_proc:
# Kill audio (will restart from correct position on resume)
try:
audio_proc.terminate()
audio_proc.wait(timeout=2)
except (subprocess.TimeoutExpired, AttributeError):
try:
audio_proc.kill()
except AttributeError:
pass
elif not paused and audio_url:
# Restart audio at current wall-clock position
audio_proc = subprocess.Popen(
[ffplay_bin, "-nodisp", "-autoexit", "-loglevel", "quiet",
"-ss", f"{play_elapsed:.1f}", audio_url],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)
# Render pause state immediately
mm, ss = divmod(int(play_elapsed), 60)
pause_indicator = " βΈ paused" if paused else ""
status = (f"\033[{term.lines};1H\033[K\033[2m"
f"[q] quit [Space] resume{pause_indicator}"
f" {mm}:{ss:02d}\033[0m")
sys.stdout.write(status)
sys.stdout.flush()
continue
if paused:
time.sleep(0.02)
continue
# --- Wall-clock sync: advance play position ---
play_elapsed += dt
target = play_elapsed * target_fps
# Read/discard frames until we reach the target frame index
latest: bytes | None = None
while frame_idx < target:
buf = ffmpeg_proc.stdout.read(frame_size)
if not buf or len(buf) < frame_size:
break
frame_idx += 1
latest = buf
if latest is not None:
last_buf = latest
lines = _render_rgb24_to_ansi(latest, pw, ph, antialias=ANTIALIAS)
prev_lines = _write_ansi_frame(lines, prev_lines, term.lines, play_elapsed)
elif last_buf and frame_idx >= target:
# Stream ended β render last frame one more time then exit
lines = _render_rgb24_to_ansi(last_buf, pw, ph, antialias=ANTIALIAS)
_write_ansi_frame(lines, prev_lines, term.lines, play_elapsed)
time.sleep(0.5)
break
# --- Throttle: sleep if we're ahead of schedule ---
elapsed_tick = time.perf_counter() - now
sleep = max(0.0, (1.0 / target_fps) - elapsed_tick)
if sleep > 0:
time.sleep(sleep)
except (KeyboardInterrupt, BrokenPipeError):
pass
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_attr)
sys.stdout.write("\033[?25h\033[?1049l")
sys.stdout.flush()
# Kill ffmpeg
if ffmpeg_proc.poll() is None:
ffmpeg_proc.terminate()
try:
ffmpeg_proc.wait(timeout=3)
except subprocess.TimeoutExpired:
ffmpeg_proc.kill()
# Kill audio
if audio_proc and audio_proc.poll() is None:
try:
audio_proc.terminate()
audio_proc.wait(timeout=3)
except subprocess.TimeoutExpired:
try:
audio_proc.kill()
except AttributeError:
pass
def cmd_watch(n_or_url: str | None = None) -> None:
"""Watch a YouTube video in-terminal as full-color ANSI art."""
state = load_state()
# Resolve input to a video URL
if n_or_url and (
n_or_url.startswith("http")
or (len(n_or_url) in (11, 34) and n_or_url.replace("_", "").isalnum())
):
url = n_or_url if n_or_url.startswith("http") else f"https://youtube.com/watch?v={n_or_url}"
_terminal_video(url)
return
# Play by index from last search
try:
n = int(n_or_url) if n_or_url else 1
except ValueError:
_eprint(f"Invalid input: {n_or_url}")
return
results = state.get("last_search", [])
if not results:
_eprint("No search results. Run 'ytm search <query>' first.")
return
if n < 1 or n > len(results):
_eprint(f"Invalid index {n}. Results are 1-{len(results)}.")
return
item = results[n - 1]
url = item["url"]
_terminal_video(url)
def cmd_mpv(n_or_url: str | None = None) -> None:
"""Open a YouTube video in mpv window for full HD playback."""
# Resolve input to a video URL
if n_or_url and (
n_or_url.startswith("http")
or (len(n_or_url) in (11, 34) and n_or_url.replace("_", "").isalnum())
):
url = n_or_url if n_or_url.startswith("http") else f"https://youtube.com/watch?v={n_or_url}"
start_mpv_video(url)
return
# Play by index from last search
try:
n = int(n_or_url) if n_or_url else 1
except ValueError:
_eprint(f"Invalid input: {n_or_url}")
return
state = load_state()
results = state.get("last_search", [])
if not results:
_eprint("No search results. Run 'ytm search <query>' first.")
return
if n < 1 or n > len(results):
_eprint(f"Invalid index {n}. Results are 1-{len(results)}.")
return
item = results[n - 1]
print(f"βΆ Opening in mpv: {item['title']}")
start_mpv_video(item["url"])
# ---------------------------------------------------------------------------
# TUI (interactive curses mode)
# ---------------------------------------------------------------------------
def cmd_tui() -> None:
"""Launch interactive TUI mode with live player display and command input."""
import curses
import io
# ββ helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _run_cmd(cmd_str: str) -> str:
"""Run a ytm command string inside TUI, capture its output."""
parts = cmd_str.strip().split()
if not parts:
return ""
c = parts[0]
a = parts[1:]
buf = io.StringIO()
old_out, old_err = sys.stdout, sys.stderr
sys.stdout = sys.stderr = buf
try:
_tui_dispatch(c, a)
except SystemExit:
pass
except Exception as e:
print(f"Error: {e}")
finally:
sys.stdout = old_out
sys.stderr = old_err
return buf.getvalue().rstrip()
def _tui_dispatch(cmd: str, args: list[str]):
"""Minimal dispatch for commands that may be called inside TUI."""
match cmd:
case "search": cmd_search(" ".join(args))
case "play": cmd_play(args[0] if args else None)
case "auto": cmd_auto(args[0] if args else None)
case "add":
for a in args:
try:
cmd_add(int(a))
except ValueError:
pass
case "add-id": cmd_add_id(args[0]) if args else None
case "remove": cmd_remove(int(args[0])) if args else None
case "next": cmd_next()
case "prev": cmd_prev()
case "stop": cmd_stop()
case "pause": cmd_pause()
case "queue": cmd_queue()
case "clear": cmd_clear()
case "loop": cmd_loop()
case "volume": cmd_volume(args)
case "status": cmd_status()
case "watch": cmd_watch(args[0] if args else None)
case "mpv": cmd_mpv(args[0] if args else None)
case "help": cmd_help()
case _:
# Bare query β search
cmd_search(" ".join([cmd] + args))
def _draw_header(win, state):
"""Draw now-playing header with cover art."""
h, w = win.getmaxyx()
playing = state.get("current", {})
title = playing.get("title", "")
if not title and mpv_running():
meta = mpv_get("metadata")
title = meta.get("title", "") if meta else ""
paused = mpv_get("pause")
vol = mpv_get("volume")
time_pos = mpv_get("time-pos")
duration = mpv_get("duration")
status_icon = "βΈ" if paused else "βΆ"
# Cover art dimensions
cover_w = min(20, w // 4 - 1)
cover_h = 6
cover_x = 1
cover_y = 1
# Info column starts right of cover
info_x = cover_x + cover_w + 2
# Draw cover art
video_id = playing.get("id", "")
if video_id:
_draw_cover(win, video_id, cover_y, cover_x, cover_w, cover_h)
# Draw now-playing info next to the cover
line = cover_y
max_info = w - info_x - 1
if title:
win.addnstr(line, info_x, f" {status_icon} {title}", max_info)
line += 1
parts = []
if time_pos is not None:
parts.append(f"{fmt_dur(time_pos)} / {fmt_dur(duration)}")
if vol is not None:
parts.append(f"π {vol}%")
if parts:
win.addnstr(line, info_x, " ".join(parts), max_info)
line += 1
else:
win.addnstr(line, info_x, " No active playback.", max_info)
line += 1
# Loop indicator
if state.get("loop"):
win.addnstr(line, info_x, " π Loop on", max_info)
line += 1
# Return next line, ensuring header is tall enough for the cover
return max(line, cover_y + cover_h) + 1
def _draw_cover(win, video_id, y, x, cols, rows):
"""Render YouTube thumbnail as half-block art at (y,x).
Each terminal cell shows one half-block char (β) where the
foreground color = top pixel and background color = bottom pixel,
giving 2Γ vertical resolution.
"""
if not video_id:
return
try:
from PIL import Image
except ImportError:
return
path = _get_thumbnail(video_id)
if path is None:
return
try:
img = Image.open(path).convert("RGB")
except Exception:
return
# resize: each char row = 2 pixel rows, each char col = 1 pixel
img = img.resize((cols, rows * 2), Image.LANCZOS)
for cy in range(rows):
for cx in range(cols):
pr = cy * 2
pb = cy * 2 + 1
r1, g1, b1 = img.getpixel((cx, pr))
r2, g2, b2 = img.getpixel((cx, pb))
t_idx = _rgb_to_ansi(r1, g1, b1)
b_idx = _rgb_to_ansi(r2, g2, b2)
pair_id = t_idx * 16 + b_idx + 1
if pair_id >= curses.COLOR_PAIRS:
pair_id = 1
try:
win.attron(curses.color_pair(pair_id))
win.addch(y + cy, x + cx, "β")
win.attroff(curses.color_pair(pair_id))
except curses.error:
pass
# ββ curses setup βββββββββββββββββββββββββββββββββββββββββββββββββ
if not sys.stdout.isatty():
print("Error: 'ytm tui' requires a real terminal.", file=sys.stderr)
return
stdscr = curses.initscr()
# ββ init colors for cover art ββ
if curses.has_colors():
curses.start_color()
curses.use_default_colors()
for fg in range(16):
for bg in range(16):
pair_id = fg * 16 + bg + 1
if pair_id < curses.COLOR_PAIRS:
curses.init_pair(pair_id, fg, bg)
# Selection highlight pair: color id 16+ (white bg, black fg) if available
try:
curses.init_color(16, 200, 200, 200)
curses.init_pair(257, curses.COLOR_BLACK, 16)
except curses.error:
curses.init_pair(257, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
stdscr.timeout(500) # non-blocking getch, redraw every 500ms for time updates
curses.curs_set(0) # hidden cursor in normal mode
# ββ TUI state ββ
mode = "normal" # "normal" | "search-input" | "search-results"
selected = 0 # selected index in the visible list
search_query = ""
search_results: list | None = None # results from last search, or None
message = ""
msg_linger = 0
running = True
try:
while running:
h, w = stdscr.getmaxyx()
state = load_state()
queue = state.get("queue", [])
current_idx = state.get("current_index", -1)
stdscr.clear()
# ββ determine visible items ββ
if mode == "search-results" and search_results:
visible = search_results
show_queue = False
else:
visible = queue
show_queue = True
if mode == "search-input":
pass # keep queue visible during typing
# Clamp selection
if visible:
selected = max(0, min(selected, len(visible) - 1))
else:
selected = 0
# ββ draw top bar ββ
if mode == "search-input":
bar = f" search: {search_query}β"
try:
stdscr.addstr(0, 0, bar[:w], curses.A_REVERSE)
except curses.error:
pass
else:
try:
stdscr.addstr(0, 0, " ytm ", curses.A_REVERSE)
# Remaining bar space shows context
paused = mpv_get("pause")
icon = "βΈ" if paused else "βΆ"
vol = mpv_get("volume")
vol_str = f"π{vol:.0f}" if vol else ""
status_parts = [icon, vol_str]
if state.get("loop"):
status_parts.append("π")
status = " ".join(p for p in status_parts if p)
if status:
try:
stdscr.addnstr(0, 5, f" {status}", w - 6, curses.A_REVERSE)
except curses.error:
pass
except curses.error:
pass
# ββ draw header (cover + now-playing) ββ
header_end = _draw_header(stdscr, state)
# ββ divider ββ
div_line = header_end
if div_line < h - 2:
try:
stdscr.addstr(div_line, 0, "β" * (w - 1), curses.A_DIM)
except curses.error:
pass
div_line += 1
# ββ draw list ββ
list_y = div_line
list_avail = h - list_y - 1 # leave footer line
if not visible:
if list_avail > 0:
msg = " Queue is empty. Press s to search."
if mode == "search-results":
msg = " No results. Press s to try again."
try:
stdscr.addnstr(list_y, 0, msg, w - 2)
except curses.error:
pass
else:
# List header
if show_queue:
label = f" Queue ({len(visible)} track{'' if len(visible) == 1 else 's'})"
else:
label = f" Search results ({len(visible)})"
try:
stdscr.addnstr(list_y, 0, label, w - 2, curses.A_BOLD)
except curses.error:
pass
list_y += 1
list_avail -= 1
# Scrolling: ensure selected is visible
scroll_offset = 0
if list_avail <= 0:
pass
elif selected >= scroll_offset + list_avail:
scroll_offset = selected - list_avail + 1
elif selected < scroll_offset:
scroll_offset = selected
for i in range(scroll_offset, min(len(visible), scroll_offset + list_avail)):
item = visible[i]
dur = fmt_dur(item.get("duration", 0))
is_sel = (i == selected)
is_current = (show_queue and i == current_idx)
marker = "βΈ" if is_current else " "
text = f" {'β' if is_sel else ' '}{marker} {i + 1}. {item['title']} [{dur}]"
text = text[:w - 1]
try:
if is_sel:
stdscr.attron(curses.color_pair(257) if curses.has_colors() else curses.A_REVERSE)
stdscr.addnstr(list_y, 0, text, w - 1)
stdscr.attroff(curses.color_pair(257) if curses.has_colors() else curses.A_REVERSE)
else:
stdscr.addnstr(list_y, 0, text, w - 1)
except curses.error:
pass
list_y += 1
# ββ draw footer / message ββ
footer_y = h - 1
if message and msg_linger > 0:
try:
stdscr.addnstr(footer_y, 0, f" {message}", w - 2)
except curses.error:
pass
msg_linger -= 1
else:
msg_linger = 0
if mode == "search-input":
hint = " Enter=search Esc=cancel"
elif mode == "search-results":
hint = " ββ Enter=play a=add A=auto-queue s=new search Esc=back"
else:
hint = " ββ Enter=play d=remove s=search v=ansi V=HD n=next"
hint += " p=pause +/-:vol l=loop A=radio q=quit"
try:
stdscr.addnstr(footer_y, 0, hint[:w - 1], curses.A_REVERSE)
except curses.error:
pass
stdscr.refresh()
# ββ input handling ββ
key = stdscr.getch()
if key == -1:
continue # timeout, redraw
if key == curses.KEY_RESIZE:
continue
if mode == "search-input":
if key == 27: # Esc
mode = "normal"
search_query = ""
search_results = None
curses.curs_set(0)
elif key in (curses.KEY_BACKSPACE, 127, 8):
if search_query:
search_query = search_query[:-1]
elif key == ord('\n'):
q = search_query.strip()
if q:
results = search_yt(q)
if results:
search_results = results
mode = "search-results"
selected = 0
search_query = ""
else:
message = "No results found."
msg_linger = 3
elif 32 <= key <= 126:
if len(search_query) < w - 20:
search_query += chr(key)
elif mode == "search-results":
if key == 27: # Esc
mode = "normal"
search_results = None
curses.curs_set(0)
elif key in (ord('j'), curses.KEY_DOWN):
if visible:
selected = min(len(visible) - 1, selected + 1)
elif key in (ord('k'), curses.KEY_UP):
selected = max(0, selected - 1)
elif key == ord('g'):
selected = 0
elif key == ord('G'):
if visible:
selected = len(visible) - 1
elif key == ord('\n') or key == ord(' '):
if visible and selected < len(visible):
item = visible[selected]
state["queue"] = []
state["current_index"] = -1
old_out, old_err = sys.stdout, sys.stderr
sys.stdout = sys.stderr = io.StringIO()
ok = _play(item, state)
sys.stdout, sys.stderr = old_out, old_err
if ok:
save_state(state)
message = f"βΆ {item['title']}"
msg_linger = 5
mode = "normal"
search_results = None
curses.curs_set(0)
elif key == ord('a'):
if visible and selected < len(visible):
item = visible[selected]
q = state.get("queue", [])
q.append(item)
state["queue"] = q
if len(q) == 1:
state["current_index"] = 0
old_out, old_err = sys.stdout, sys.stderr
sys.stdout = sys.stderr = io.StringIO()
ok = _play(item, state)
sys.stdout, sys.stderr = old_out, old_err
message = f"β +βΆ {item['title']}" if ok else f"β {item['title']}"
else:
message = f"β {item['title']}"
save_state(state)
msg_linger = 3
elif key == ord('A'):
if visible and selected < len(visible):
item = visible[selected]
old_out, old_err = sys.stdout, sys.stderr
sys.stdout = sys.stderr = io.StringIO()
cmd_auto(item["id"])
sys.stdout, sys.stderr = old_out, old_err
message = f"π» Auto-queued from: {item['title']}"
msg_linger = 5
mode = "normal"
search_results = None
curses.curs_set(0)
elif key == ord('s'):
mode = "search-input"
search_query = ""
search_results = None
curses.curs_set(1)
else: # normal mode
if key == ord('q'):
running = False
elif key in (ord('j'), curses.KEY_DOWN):
if visible:
selected = min(len(visible) - 1, selected + 1)
elif key in (ord('k'), curses.KEY_UP):
selected = max(0, selected - 1)
elif key == ord('g'):
selected = 0
elif key == ord('G'):
if visible:
selected = len(visible) - 1
elif key in (ord('s'), ord('/'), ord('S')):
mode = "search-input"
search_query = ""
search_results = None
curses.curs_set(1)
elif key == ord('v'):
# Watch video in-terminal (ANSI art)
if visible and selected < len(visible):
item = visible[selected]
vid = item.get("id", "") or item.get("url", "")
if vid:
curses.endwin()
url = vid if vid.startswith("http") else f"https://youtube.com/watch?v={vid}"
_terminal_video(url)
# Re-enter curses
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
stdscr.timeout(500)
if curses.has_colors():
curses.start_color()
curses.use_default_colors()
for fg in range(16):
for bg in range(16):
pair_id = fg * 16 + bg + 1
if pair_id < curses.COLOR_PAIRS:
curses.init_pair(pair_id, fg, bg)
try:
curses.init_color(16, 200, 200, 200)
curses.init_pair(257, curses.COLOR_BLACK, 16)
except curses.error:
curses.init_pair(257, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.curs_set(0)
continue
elif key == ord('V'):
# Open video in mpw window (full HD)
if visible and selected < len(visible):
item = visible[selected]
vid = item.get("id", "") or item.get("url", "")
if vid:
url = vid if vid.startswith("http") else f"https://youtube.com/watch?v={vid}"
print(f"βΆ Opening in mpv: {item.get('title', '')}")
start_mpv_video(url)
elif key == ord('\n'):
if visible and selected < len(visible):
item = visible[selected]
if show_queue:
# Play this queue item from its current position
state["current_index"] = selected
state["current"] = item
old_out, old_err = sys.stdout, sys.stderr
sys.stdout = sys.stderr = io.StringIO()
ok = _play(item, state)
sys.stdout, sys.stderr = old_out, old_err
if ok:
save_state(state)
message = f"βΆ {item['title']}"
msg_linger = 5
else:
# Search result β play it
state["queue"] = []
state["current_index"] = -1
old_out, old_err = sys.stdout, sys.stderr
sys.stdout = sys.stderr = io.StringIO()
ok = _play(item, state)
sys.stdout, sys.stderr = old_out, old_err
if ok:
save_state(state)
message = f"βΆ {item['title']}"
msg_linger = 5
mode = "normal"
search_results = None
curses.curs_set(0)
message = f"βΆ {item['title']}"
msg_linger = 5
elif key == ord('d') or key == ord('x'):
if show_queue and visible and selected < len(visible):
item = visible[selected]
idx = selected
if 0 <= idx < len(queue):
q = list(queue)
removed = q.pop(idx)
state["queue"] = q
if current_idx > idx or current_idx == len(q):
state["current_index"] = max(-1, current_idx - 1)
save_state(state)
if selected >= len(q) and selected > 0:
selected -= 1
message = f"β Removed: {removed['title']}"
msg_linger = 3
elif key == ord('n'):
state = load_state()
q = state.get("queue", [])
ci = state.get("current_index", -1)
if ci + 1 < len(q):
_tui_dispatch("next", [])
message = "β Next track"
else:
message = "End of queue"
msg_linger = 3
elif key == ord('p') or key == 32: # p or Space
_tui_dispatch("pause", [])
paused = mpv_get("pause")
message = "βΈ Paused" if paused else "βΆ Resumed"
msg_linger = 3
elif key == ord('A'):
state = load_state()
current = state.get("current")
if current:
vid = current.get("id")
if vid:
old_out, old_err = sys.stdout, sys.stderr
sys.stdout = sys.stderr = io.StringIO()
related = get_related(vid)
sys.stdout, sys.stderr = old_out, old_err
if related:
state["queue"].extend(related)
save_state(state)
message = f"π» Radio on: {len(related)} related songs queued"
else:
message = "No related songs found."
else:
message = "Nothing playing to base radio on."
msg_linger = 5
elif key == ord('l'):
state = load_state()
loop = not state.get("loop", False)
state["loop"] = loop
save_state(state)
message = "π Loop: on" if loop else "π Loop: off"
msg_linger = 3
elif key in (ord('+'), ord('=')):
vol = mpv_get("volume") or 50
new_vol = min(100, vol + 10)
_mpv_send({"command": ["set_property", "volume", new_vol]})
message = f"π Volume: {new_vol:.0f}%"
msg_linger = 3
elif key == ord('-') or key == ord('_'):
vol = mpv_get("volume") or 50
new_vol = max(0, vol - 10)
_mpv_send({"command": ["set_property", "volume", new_vol]})
message = f"π Volume: {new_vol:.0f}%"
msg_linger = 3
elif key in (12, ord('r')): # Ctrl+L or r = refresh
pass
except KeyboardInterrupt:
pass
finally:
try:
curses.curs_set(0)
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()
except (curses.error, NameError):
pass
# Clean exit message
print("ytm TUI exited. Run 'ytm tui' to reopen.")
def cmd_help() -> None:
print(textwrap.dedent("""\
ytm β CLI YouTube Music Player
Search, queue, and play YouTube audio through mpv.
All operations are non-interactive shell commands.
USAGE
ytm tui Launch interactive TUI (text user interface)
ytm search <query> Search YouTube and print numbered results
ytm play [n|url|id] Play search result #n (default 1), or a URL/ID
ytm auto [n|url|id] Play + auto-queue related songs via YouTube Mix
ytm watch [n|url|id] Watch video in-terminal as ANSI art
ytm mpv [n|url|id] Open video in mpv window for full HD
ytm add <n> Add search result #n to the queue
ytm add-id <url|id> Add a specific video by URL/ID to the queue
ytm remove <n> Remove item #n from the queue
ytm next Skip to next track in queue
ytm prev Previous track
ytm stop Stop playback and clear queue
ytm pause Toggle play/pause
ytm queue Show current queue and now-playing
ytm clear Clear the queue
ytm volume [0-100] Get or set volume
ytm loop Toggle queue looping
ytm status Show now-playing and player state
ytm help Show this help
ytm <query> Shorthand: search + interactive pick (fzf)
EXAMPLES
ytm tui # interactive mode
ytm search akon # find songs
ytm play 2 # play result #2
ytm add 1 2 3 # queue results 1-3
ytm add-id dQw4w9WgXcQ # queue by video ID
ytm auto 2 # play result #2 + auto-queue related
ytm remove 2 # remove from queue
ytm next # skip track
ytm volume 50 # set volume to 50%
DEPENDENCIES
mpv, yt-dlp, ffmpeg # required
fzf # optional (interactive mode)
FILES
~/.cache/ytm/state.json # queue, search, settings
/tmp/ytm-mpv-socket # mpv IPC socket
"""))
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def _eprint(*args, **kwargs) -> None:
print(*args, file=sys.stderr, **kwargs)
def main() -> None:
if len(sys.argv) < 2:
cmd_help()
return
cmd = sys.argv[1]
args = sys.argv[2:]
match cmd:
case "search":
if args:
cmd_search(" ".join(args))
else:
_eprint("Usage: ytm search <query>")
case "play":
cmd_play(args[0] if args else None)
case "auto":
cmd_auto(args[0] if args else None)
case "add":
if args:
for a in args:
try:
cmd_add(int(a))
except ValueError:
_eprint(f"Invalid index: {a}")
else:
_eprint("Usage: ytm add <n> [n ...]")
case "add-id":
if args:
cmd_add_id(args[0])
else:
_eprint("Usage: ytm add-id <url|id>")
case "next":
cmd_next()
case "prev":
cmd_prev()
case "stop":
cmd_stop()
case "pause":
cmd_pause()
case "queue":
cmd_queue()
case "remove":
if args:
try:
cmd_remove(int(args[0]))
except ValueError:
_eprint("Usage: ytm remove <n>")
else:
_eprint("Usage: ytm remove <n>")
case "clear":
cmd_clear()
case "loop":
cmd_loop()
case "volume":
cmd_volume(args)
case "status":
cmd_status()
case "watch":
cmd_watch(args[0] if args else None)
case "mpv":
cmd_mpv(args[0] if args else None)
case "tui" | "interactive":
cmd_tui()
case "help":
cmd_help()
case _:
# Bare query: try fzf interactive, fall back to print results
query = " ".join(sys.argv[1:])
results = search_yt(query)
if not results:
return
if FZF:
items = [fmt_item(r) for r in results]
try:
r = subprocess.run(
[FZF, "--prompt", "Play: "],
input="\n".join(items),
capture_output=True,
text=True,
timeout=30,
)
if r.returncode == 0 and r.stdout.strip():
sel = r.stdout.strip()
for i, it in enumerate(items):
if it == sel:
state = load_state()
state["queue"] = []
state["current_index"] = -1
_play(results[i], state)
save_state(state)
return
except (subprocess.TimeoutExpired, FileNotFoundError):
pass
# Fallback: print numbered results
for i, r in enumerate(results, 1):
print(f" {i:2d}. {fmt_item(r)}")
_eprint("\nRun 'ytm play <n>' to play a result.")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print()
except BrokenPipeError:
pass
|