-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadl.sh
executable file
·91 lines (76 loc) · 2.48 KB
/
adl.sh
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
#!/bin/bash
# archive.org stream downloader
# Downloads segmented video streams from archive.org and merges them into a single video file.
# Display help information
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
cat << EOF
Usage:
$0 [URL] [OUTPUT_DIRECTORY]
Description:
Downloads video streams from archive.org and merges them into a single video file.
EOF
exit 0
fi
# Check for correct number of arguments and valid URL format
#if [ "$#" -eq 0 ] || ! [[ $# =~ ^https?://.*archive\\.org/ ]]; then
# echo "Error: A valid URL starting with 'http://' or 'https://' and containing 'archive.org/' is required." >&2
# exit 1
#fi
# Validate necessary commands are installed
check_dependency() {
if ! command -v "$1" &> /dev/null; then
echo "Error: Required command '$1' is not installed or not in PATH." >&2
return 1
fi
}
check_dependency wget || exit 1
check_dependency xidel || exit 1
check_dependency ffmpeg || exit 1
# Set default output directory
DEFAULT_OUTPUT='/mnt/news/'
OUTPUT=''
if [ -d "$DEFAULT_OUTPUT" ]; then
OUTPUT="$DEFAULT_OUTPUT"
elif [ -d "$2" ]; then
OUTPUT="$2"
else
echo 'Error: No output directory found.'
exit 1
fi
echo "Output directory: $OUTPUT"
# Create a temporary folder
TEMPORARY=$(mktemp -d) && echo "Saving temporary files to $TEMPORARY"
# Get the list of segments for the URL provided
LINKS=$(xidel -s $1 -e "json(//input[@class='js-tv3-init']/@value)/(TV3.clipstream_clips)()")
# Verify URL and prepare filename
URL=$(echo $1 | awk -F 'details/' '{print $2}' | awk -F '/' '{print $1}')
if [ -z "$URL" ]; then
echo "Error: URL was malformed after it was processed."
exit 1
fi
# Download the segments to the temporary folder
if [ -n "$LINKS" ]; then
COUNTER=0
for i in $LINKS; do
echo "Downloading $i"
wget "$i" -O "$TEMPORARY"/"$URL"_"$COUNTER".mp4 || {
echo "Download failed for $i" >&2
exit 1
}
((COUNTER++))
done
else
echo "Error: Couldn't parse links."
exit 1
fi
# Create a sequential list of the segments in the folder:
find "$TEMPORARY"/*.mp4 | sed 's:\\ :\\\\ :g' | sed 's/^/file /' > "$TEMPORARY"/segments.txt
sort --version-sort "$TEMPORARY"/segments.txt > "$TEMPORARY"/sorted.txt
echo "Merging segments..."
ffmpeg -f concat -safe 0 -i "$TEMPORARY"/sorted.txt -c copy "$OUTPUT/$URL".mp4 || {
echo "Error during merge."
exit 1
}
# Cleanup temporary files
echo "Removing temporary files in $TEMPORARY..."
rm -Rf "$TEMPORARY"