<commit_before>#!/usr/bin/env python

import io
import shlex
import subprocess

from flask import Flask, request, send_file

app = Flask(__name__)

@app.route('/<path:url>')
def snapshot(url):
    if request.query_string:
        url += '?' + request.query_string
    # TODO: Sanitize interpolated string
    cmd = 'ffmpeg -i "rtsp://%s" -hide_banner -loglevel quiet -ss 00:00:01.500 -f image2 -vframes 1 -y -' % (url,)
    p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
    p.wait()
    image = p.stdout.read()
    return send_file(io.BytesIO(image),
                     attachment_filename='snapshot.jpg',
                     mimetype='image/jpeg')

if __name__ == '__main__':
    app.run(debug=True, port=5000)
<commit_msg>Added support for running on FreeBSD, because subprocess.Popen doesn't behave like on a GNU/Linux platform.<commit_after>#!/usr/bin/env python

import io
import platform
import shlex
import subprocess

from flask import Flask, request, send_file, send_from_directory

app = Flask(__name__)

tmp_dir = '/tmp/'
tmp_filename = 'snapshot.jpg'

@app.route('/<path:url>')
def snapshot(url):
    freebsd_platform = platform.system() == 'FreeBSD'
    if request.query_string:
        url += '?' + request.query_string
    # TODO: Sanitize interpolated string
    cmd = 'ffmpeg -rtsp_transport tcp -i "rtsp://%s" -hide_banner -loglevel quiet -ss 00:00:01.500 -f image2 -vframes 1 -y ' % (url,)
    if freebsd_platform:
        cmd += tmp_dir + tmp_filename
    else:
        cmd += '-'
    p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
    p.wait()
    image = p.stdout.read()
    if freebsd_platform:
        return send_from_directory(tmp_dir, tmp_filename)
    return send_file(io.BytesIO(image),
                     attachment_filename='snapshot.jpg',
                     mimetype='image/jpeg')

if __name__ == '__main__':
    app.run(debug=True, port=5000)

{'fraction_non_alphanumeric': 0.1289144050104384, 'fraction_numerical': 0.015657620041753653, 'mean_word_length': 3.1493506493506493, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4392734', 'n_tokens_mistral': 676, 'n_tokens_neox': 639, 'n_words': 152}